丝丝爱,国模安可,毁灭前夕 倒数救援
在做多语言版本的时候,日期时间的格式话是一个很头疼的事情,幸好android提供了dateformate,可以根据指定的语言区域的默认格式来格式化。
直接贴代码:
final context context, final long time, final boolean simple,
locale locale) {
final gregoriancalendar now = new gregoriancalendar();
// special time
if (time < millseconds_of_hour) {
return "";
}
// today
final gregoriancalendar today = new gregoriancalendar(
now.get(gregoriancalendar.year),
now.get(gregoriancalendar.month),
now.get(gregoriancalendar.day_of_month));
final long in24h = time - today.gettimeinmillis();
if (in24h > 0 && in24h <= millseconds_of_day) {
java.text.dateformat df = java.text.dateformat.gettimeinstance(
java.text.dateformat.short, locale);
return "" + df.format(time);
}
// yesterday
final long in48h = time - today.gettimeinmillis() + millseconds_of_day;
if (in48h > 0 && in48h <= millseconds_of_day) {
return simple ? context.getstring(r.string.fmt_pre_yesterday)
: context.getstring(r.string.fmt_pre_yesterday)
+ " "
+ java.text.dateformat.gettimeinstance(
java.text.dateformat.short, locale).format(
time);
}
final gregoriancalendar target = new gregoriancalendar();
target.settimeinmillis(time);
// same week
if (now.get(gregoriancalendar.year) == target
.get(gregoriancalendar.year)
&& now.get(gregoriancalendar.week_of_year) == target
.get(gregoriancalendar.week_of_year)) {
java.text.simpledateformat sdf = new java.text.simpledateformat("e", locale);
final string dow = "" + sdf.format(time);
return simple ? dow : dow
+ java.text.dateformat.gettimeinstance(
java.text.dateformat.short, locale).format(time);
}
// same year
if (now.get(gregoriancalendar.year) == target
.get(gregoriancalendar.year)) {
return simple ? java.text.dateformat.getdateinstance(
java.text.dateformat.short, locale).format(time)
: java.text.dateformat.getdatetimeinstance(
java.text.dateformat.short,
java.text.dateformat.short, locale).format(time);
}
return simple ? java.text.dateformat.getdateinstance(
java.text.dateformat.short, locale).format(time)
: java.text.dateformat.getdatetimeinstance(
java.text.dateformat.short, java.text.dateformat.short,
locale).format(time);
}
详细介绍见:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论