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

首頁 > 系統 > Android > 正文

Android之日期時間格式的實現方法

2020-02-21 17:34:18
字體:
來源:轉載
供稿:網友

計算機語言多種多樣,它的日期和時間格式通常會令程序員頭疼,但是幸運的是,android提供了dateformate,它可以根據指定語言區域的默認格式進行格式化,下面是武林技術頻道小編介紹的Android之日期時間格式的實現方法,一起來看看吧!

直接貼代碼:

復制代碼 代碼如下:

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。

以上代碼都是武林技術頻道小編為大家搜集的Android之日期時間格式的實現方法,希望對大家的學習有一定的幫助!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开原市| 和硕县| 盐亭县| 垦利县| 桂林市| 内江市| 清原| 上蔡县| 连州市| 旬邑县| 同德县| 元朗区| 广安市| 合阳县| 建宁县| 时尚| 荣昌县| 丰镇市| 津市市| 勃利县| 安塞县| 南涧| 本溪市| 鸡西市| 宕昌县| 张家口市| 兴城市| 鄢陵县| 昌图县| 合阳县| 罗江县| 湛江市| 金堂县| 福清市| 抚顺市| 荣昌县| 平武县| 临桂县| 高密市| 抚顺市| 永靖县|