欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2013年05月10日 10:35:25   作者:  
本篇文章是對(duì)在Android中 日期時(shí)間格式國際化的實(shí)現(xiàn)代碼進(jìn)行了分析介紹。需要的朋友參考下

在做多語言版本的時(shí)候,日期時(shí)間的格式話是一個(gè)很頭疼的事情,幸好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,還有另外一個(gè)java.text.format.DateFormat,后者不能指定locale。

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

相關(guān)文章

  • 淺談Android View滑動(dòng)沖突的解決方法

    淺談Android View滑動(dòng)沖突的解決方法

    本篇文章主要介紹了淺談Android View滑動(dòng)沖突的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • Android Compose衰減動(dòng)畫Animatable使用詳解

    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é)

    這篇文章主要介紹了淺談Android客戶端與服務(wù)器的數(shù)據(jù)交互總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • android讀取掃碼模組數(shù)據(jù)的方法

    android讀取掃碼模組數(shù)據(jù)的方法

    這篇文章主要為大家詳細(xì)介紹了android讀取掃碼模組數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android仿QQ、微信聊天界面長按提示框效果

    Android仿QQ、微信聊天界面長按提示框效果

    最近在工作項(xiàng)目中要實(shí)現(xiàn)一個(gè)長按提示 “復(fù)制” 的功能,類似于QQ、微信聊天界面長按提示框效果,本來想偷懶在網(wǎng)上找個(gè)開源的項(xiàng)目用,但是看了好幾個(gè)都不是很滿意,所以就打算按照自己的思路來實(shí)現(xiàn)一個(gè)。下面分享給大家,有需要的朋友們可以參考借鑒。
    2016-11-11
  • Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫(2)

    Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫(2)

    這篇文章主要為大家詳細(xì)介紹了Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Flutter通過Container實(shí)現(xiàn)時(shí)間軸效果

    Flutter通過Container實(shí)現(xiàn)時(shí)間軸效果

    時(shí)間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實(shí)現(xiàn),感興趣的朋友可以了解下
    2021-05-05
  • android長截屏原理及實(shí)現(xiàn)代碼

    android長截屏原理及實(shí)現(xiàn)代碼

    本篇文章主要介紹了android長截屏原理及實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Android Studio徹底刪除項(xiàng)目 Android Studio徹底刪除Module

    Android Studio徹底刪除項(xiàng)目 Android Studio徹底刪除Module

    這篇文章主要為大家詳細(xì)介紹了Android Studio徹底刪除項(xiàng)目,Android Studio徹底刪除Module,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android OpenGLES2.0繪制三角形(二)

    Android OpenGLES2.0繪制三角形(二)

    這篇文章主要為大家詳細(xì)介紹了Android OpenGLES2.0繪制三角形的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評(píng)論