Android日期时间格式国际化的实现代码

2016-02-19 10:01 2 1 收藏

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的Android日期时间格式国际化的实现代码,过去的都会过去,迎接崭新的开始,释放更美好的自己。

【 tulaoshi.com - 编程语言 】

在做多语言版本的时候,日期时间的格式话是一个很头疼的事情,幸好Android提供了DateFormate,可以根据指定的语言区域的默认格式来格式化。

直接贴代码:
代码如下:

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(

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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(

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

java.text.DateFormat.SHORT, locale).format(time)

: java.text.DateFormat.getDateTimeInstance(

java.text.DateFormat.SHORT, java.text.DateFormat.SHORT,

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

locale).format(time);

}

注意这里用的是java.text.DateFormat,还有另外一个java.text.format.DateFormat,后者不能指定locale。

详细介绍见:http://developer.android.com/reference/java/text/DateFormat.html

来源:https://www.tulaoshi.com/n/20160219/1593021.html

延伸阅读
文件main.java 代码如下: package com.HHBrowser.android; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.EditText;...
关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较好,不偏心了,下面看几个示例:               首先说一下:拖拽ListView的item就不应该可以任意移动,只应该在ListView所在的范围内,而网易的你看看我都...
如果一个应用程序中的数据库无需提供对外访问,实现一个继承自SQLiteOpenHelper的数据库帮助类,以支持数据库的创建和版本的更新, 这些SQLiteDataBase所不能实现的.但是SQLiteDataBase却具备一些非常重要的对数据库进行操作的方法,数据表的创建删除、数据 的增删改查都是通过它实现的。 执行增删改操作方法 :db.execSQL(sql); 或者db.inser...
代码如下: import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.json.J...
标签: Web开发
jQuery国际化插件可以让你在JavaScript代码中轻松地处理不同文化之间的数字、货币和日期格式。例如,你可以使用这个插件正确地显示一个国家的货币符号 上个月,我写了一篇关于微软如何在向 jQuery 贡献代码的文章,也谈到了在第一批贡献的代码中的一些功能:jQuery模板和数据链接支持. 今天,我们发布了一个新的jQuery国际化插件的原型,你...

经验教程

559

收藏

68
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部