Android⽇期时间格式国际化
在做多语⾔版本的时候,⽇期时间的格式话是⼀个很头疼的事情,幸好Android提供了DateFormate,可以根据指定的语⾔区域的默认格式来格式化。直接贴代码:
public static CharSequence formatTimeInListForOverSeaUser(
final Context context, final long time, final boolean simple,
Locale locale) {
java时间日期格式转换final GregorianCalendar now = new GregorianCalendar();
// special time
if (time < MILLSECONDS_OF_HOUR) {
return "";
}
// today
final GregorianCalendar today = new GregorianCalendar(
<(GregorianCalendar.YEAR),
<(GregorianCalendar.MONTH),
<(GregorianCalendar.DAY_OF_MONTH));
final long in24h = time - TimeInMillis();
if (in24h > 0 && in24h <= MILLSECONDS_OF_DAY) {
return "" + df.format(time);
}
// yesterday
final long in48h = time - TimeInMillis() + MILLSECONDS_OF_DAY;
if (in48h > 0 && in48h <= MILLSECONDS_OF_DAY) {
return simple ? String(R.string.fmt_pre_yesterday)
: String(R.string.fmt_pre_yesterday)
+ " "
+ TimeInstance(
time);
}
final GregorianCalendar target = new GregorianCalendar();
target.setTimeInMillis(time);
/
/ same week
if ((GregorianCalendar.YEAR) == target
.get(GregorianCalendar.YEAR)
&& (GregorianCalendar.WEEK_OF_YEAR) == target
.get(GregorianCalendar.WEEK_OF_YEAR)) {
final String dow = "" + sdf.format(time);
return simple ? dow : dow
+ TimeInstance(
}
/
/ same year
if ((GregorianCalendar.YEAR) == target
.get(GregorianCalendar.YEAR)) {
return simple ? DateInstance(
: DateTimeInstance(
}
return simple ? DateInstance(
:
DateTimeInstance(
locale).format(time);
}
注意这⾥⽤的是DateFormat,还有另外⼀个format.DateFormat,后者不能指定locale。详细介绍见:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。