Android日期时间格式国际化的实现代码
发布时间:2023-08-28 04:44:42
在做多语言版本的时候,日期时间的格式话是一个很头疼的事情,幸好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(
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。
详细介绍见:http://developer.android.com/reference/java/text/DateFormat.html
猜你喜欢
- 大家好,今天给大家带来一个仿360手机卫士悬浮窗效果的教程,在开始之前请允许我先说几句不相干的话。不知不觉我发现自己接触Android已有近
- 在本文中,我们将看到几个关于如何在Java 8中对List进行排序的示例。1.按字母顺序排序字符串列表List<String>
- 前言:对于一个程序员来说,尤其是在java web端开发的程序员,三大框架:Struts+Hibernate+Spring是必须要掌握熟透的
- spring data jpa使用自定义repository实现类spring data jpa中使用JpaRepository等接口定义r
- 一、 WillPopScope用法WillPopScope本质是一个widget用于拦截物理按键返回事件(Android的物理返回键和iOS
- 一、使用@CrossOrigin注解在controller类上加上@CrossOrigin注解,就能对这个类下面所有方法进行跨域访问了@Cr
- 1. matlab的lp2lp函数的作用去归一化 H(s) 的分母2. matlab的lp2lp函数的使用方法[z, p, k]=butta
- JLabel 对象可以显示文本、图像或同时显示二者。可以通过设置垂直和水平对齐方式,指定标签显示区中标签内容在何处对齐。默认情况下,标签在其
- 参考: https://gist.github.com/laispace/666dd7b27e9116faece6前提是你本地有 socks
- from jnius import autoclass>>> Stack = autoclass('java.ut
- 1、右值1.1 简介首先区分一下左右值:左值是指存储在内存中、有明确存储地址(可取地址)的数据;右值是指可以提供数据值的数据(不可取地址)如
- 本文研究的主要是Java回调函数与观察者模式的实现,具体介绍和实现代码如下。观察者模式(有时又被称为发布(publish )-订阅(Subs
- 前言:项目开发中日志是不可缺少的一部分,通过日志能够定位和分析事故原因。目前流行日志框架包含了log4j、log4j2、logback等,另
- 最近遇到了一个问题,一份很老的代码要修改里面的变量,源码早就和开发者一起不知去向,其中引用了一些jar包导致无法直接编译,只能直接修改.cl
- 1.使用WINDOWS API/// <summary> /// 判断一个磁盘驱动器的类型 /// </summary&g
- 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发MyBa
- 1、采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。sp
- 前言在手写sql的时候,根据参数处理查询条件.select * from staff where 1 = 1 and staff_id in
- 如果想实现一个在桌面显示的悬浮窗,用Dialog、PopupWindow、Toast等已经不能实现了,他们基本都是在Activity之上显示
- 背景今天学习Springboot,但是用的apache-maven 3.0 ,导入springboot1.5.19 ,Maven项目老是爆红