gpt4 book ai didi

android - DateUtils.getRelativeTimeSpanString 在 android 中以其他语言强制使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:06 27 4
gpt4 key购买 nike

我正在尝试在 android 中实现 DateUtils.getRelativeTimeSpanString 但我想以意大利语显示数据,即使手机的默认语言设置为英语我想显示数据,即“1 ora fa”而不是“1 分钟前”

我正在为此使用以下代码

DateUtils.  getRelativeTimeSpanString (currenttimeMillis, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS).toString().toLowerCase(Locale.ITALIAN)

最佳答案

因为我不知道如何格式化 DateUtils.getRelativeTimeSpanString 的语言环境,而且很难手动更改默认用户设备提供的语言。我使用 TimeUnit 制作了这个方法。

我在这里为您简化了操作,只需使用此方法并更改将在您的语言中使用的时区、日期格式和字符串。如果您有任何问题或我遗漏了什么,请告诉我。

public static String getRelativeTimeSpanString(String time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH);//change this format if you don't want to use this format
sdf.setTimeZone(TimeZone.getTimeZone("GMT+7"));//change this to the timezone you use
Date datePast = sdf.parse(time);
Date dateNow = new Date();
//here you can change the language to match your language
String sAgo = "yang lalu";
String sMilli = "mili detik";
String sSecond = "detik";
String sMinute = "menit";
String sHour = "jam";
String sDay = "hari";
String sWeek = "minggu";
String sMonth = "bulan";
String sYear = "tahun";

long lDay = TimeUnit.MILLISECONDS.toDays(dateNow.getTime() - datePast.getTime());
if (lDay == 0) {
long lHour = TimeUnit.MILLISECONDS.toHours(dateNow.getTime() - datePast.getTime());
if (lHour == 0) {
long lMinute = TimeUnit.MILLISECONDS.toMinutes(dateNow.getTime() - datePast.getTime());
if (lMinute == 0) {
long lSecond = TimeUnit.MILLISECONDS.toSeconds(dateNow.getTime() - datePast.getTime());
if (lSecond == 0) {
long lMilliSecond = TimeUnit.MILLISECONDS.toMillis(dateNow.getTime() - datePast.getTime());
if (lMilliSecond == 0) {
return time;
} else {
return lMilliSecond + " " + sMilli + " " + sAgo;
}
} else {
return lSecond + " " + sSecond + " " + sAgo;
}
} else {
return lMinute + " " + sMinute + " " + sAgo;
}
} else {
return lHour + " " + sHour + " " + sAgo;
}
} else {
if (lDay < 7) {
return lDay + " " + sDay + " " + sAgo;
} else {
long lWeek = lDay / 7;
if (lWeek < 4) {
return lWeek + " " + sWeek + " " + sAgo;
} else if (lWeek == 4) {
return 1 + sMonth + " " + sAgo;
} else {
long lMonth = lDay / 30;
if (lMonth < 12) {
return lMonth + " " + sMonth + " " + sAgo;
} else if (lMonth == 12) {
return 1 + sYear + " " + sAgo;
} else {
long lYear = lDay / 365;
return lYear + " " + sYear + " " + sAgo;
}
}
}
}
} catch (Exception e) {
return time;
}
}

也可以使用此方法更改配置的区域设置

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void setDefaultLocale() {
//call this method from application scope
Configuration configuration = new Configuration(Resources.getSystem().getConfiguration());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.locale = Locale.ENGLISH; // or whichever locale you desire
} else {
configuration.setLocale(Locale.ENGLISH);
}
Resources.getSystem().updateConfiguration(configuration, null);
}

但我不认为更改语言环境是个好主意。

关于android - DateUtils.getRelativeTimeSpanString 在 android 中以其他语言强制使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28697738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com