国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android日期時間格式國際化的實(shí)現(xiàn)代碼

2020-04-11 12:24:52
字體:
供稿:網(wǎng)友

在做多語言版本的時候,日期時間的格式話是一個很頭疼的事情,幸好Android提供了DateFormate,可以根據(jù)指定的語言區(qū)域的默認(rèn)格式來格式化。

直接貼代碼:

復(fù)制代碼 代碼如下:

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,還有另外一個java.text.format.DateFormat,后者不能指定locale。

詳細(xì)介紹見:http://developer.android.com/reference/java/text/DateFormat.html

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金门县| 志丹县| 上饶市| 桓仁| 灵璧县| 宜昌市| 镇安县| 榕江县| 桂林市| 金川县| 曲阜市| 永济市| 兴山县| 蚌埠市| 黎平县| 洪湖市| 饶河县| 通河县| 吉木萨尔县| 金昌市| 双牌县| 阳春市| 孟州市| 闽侯县| 吉隆县| 休宁县| 同德县| 翁牛特旗| 巴青县| 镇巴县| 贞丰县| 汽车| 迁西县| 无棣县| 奉节县| 吉安县| 博爱县| 芦山县| 克山县| 临海市| 武穴市|