Android日期時(shí)間格式國際化的實(shí)現(xiàn)代碼
在做多語言版本的時(shí)候,日期時(shí)間的格式話是一個(gè)很頭疼的事情,幸好Android提供了DateFormate,可以根據(jù)指定的語言區(qū)域的默認(rèn)格式來格式化。
直接貼代碼:
public static CharSequence formatTimeInListForOverSeaUser(
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);
}
注意這里用的是java.text.DateFormat,還有另外一個(gè)java.text.format.DateFormat,后者不能指定locale。
詳細(xì)介紹見:http://developer.android.com/reference/java/text/DateFormat.html
- 解析android中系統(tǒng)日期時(shí)間的獲取
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android中日期與時(shí)間設(shè)置控件用法實(shí)例
- Android開發(fā)之時(shí)間日期操作實(shí)例
- Android開發(fā)之時(shí)間日期組件用法實(shí)例
- Android之日期及時(shí)間選擇對(duì)話框用法實(shí)例分析
- Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼
- Android仿iPhone日期時(shí)間選擇器詳解
- Android開發(fā)中DatePicker日期與時(shí)間控件實(shí)例代碼
- Android 日期和時(shí)間的使用實(shí)例詳解
- Android開發(fā)獲取當(dāng)前系統(tǒng)日期和時(shí)間功能示例
相關(guān)文章
Android Compose衰減動(dòng)畫Animatable使用詳解
這篇文章主要為大家介紹了Android Compose衰減動(dòng)畫Animatable使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11淺談Android客戶端與服務(wù)器的數(shù)據(jù)交互總結(jié)
這篇文章主要介紹了淺談Android客戶端與服務(wù)器的數(shù)據(jù)交互總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫(2)
這篇文章主要為大家詳細(xì)介紹了Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Flutter通過Container實(shí)現(xiàn)時(shí)間軸效果
時(shí)間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實(shí)現(xiàn),感興趣的朋友可以了解下2021-05-05Android Studio徹底刪除項(xiàng)目 Android Studio徹底刪除Module
這篇文章主要為大家詳細(xì)介紹了Android Studio徹底刪除項(xiàng)目,Android Studio徹底刪除Module,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04