Android日期時(shí)間格式國(guó)際化的實(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控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能
這篇文章主要為大家詳細(xì)介紹了Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
關(guān)于Android Fragment對(duì)回退棧的詳細(xì)理解
這篇文章主要介紹了Android Fragment的回退棧示例詳細(xì)介紹的相關(guān)資料,在Android中Fragment回退棧是由Activity管理的,每個(gè)Activity都有自己的回退棧,其中保存了已經(jīng)停止(處于后臺(tái))的Fragment實(shí)例,需要的朋友可以參考下2016-12-12
Android UI設(shè)計(jì)之AlertDialog彈窗控件
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)之AlertDialog彈窗控件的使用方法,感興趣的小伙伴們可以參考一下2016-08-08
Android開發(fā)之5.0activity跳轉(zhuǎn)時(shí)共享元素的使用方法
下面小編就為大家分享一篇Android開發(fā)之5.0activity跳轉(zhuǎn)時(shí)共享元素的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android實(shí)現(xiàn)笑臉進(jìn)度加載動(dòng)畫
這篇文章主要介紹了Android實(shí)現(xiàn)笑臉進(jìn)度加載動(dòng)畫的方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-05-05

