格式化时间的函数

/**

* @param timeMills
*            格式化时间
* @param currentMills
*            当前时间
* @return 时间日期显示,返回的格式有yy-MM-dd,星期几 如果要显示几天前,几分钟前,几秒前请使用DateUtils的相关方法
*/
public static String formatDateDisplayStyle(long date, long currentMills, Context context) {
Resources r = Resources.getSystem();
Locale locale = Locale.getDefault();
SimpleDateFormat sdf = null;
String returnStr = null;
int resId;
boolean past = (currentMills >= date);
long duration = currentMills - date;
long absDuration = Math.abs(duration);
returnStr += "past=" + past + "||absDuration=" + absDuration;
//如果大于当前时间或者在一个星期之前
if (!past || absDuration > WEEK_IN_MILLIS) {
if (locale.equals(Locale.CHINA)) {
sdf = new SimpleDateFormat("yy-M-d");
} else {
sdf = new SimpleDateFormat("M/d/yy");
}
returnStr = sdf.format(new Date(date));
return returnStr;
} else {
//如果是在24小时之内
if (absDuration < DAY_IN_MILLIS) {
Log.i("liyong","===24小时之内");
if (DateFormat.is24HourFormat(context)) {
Log.i("liyong", "====24小时制");
sdf = new SimpleDateFormat("H:MM");
} else {
locale.setDefault(Locale.ENGLISH);
if (locale.equals(Locale.CHINA)) {
Log.i("liyong", "====12中国片区");
sdf = new SimpleDateFormat("a hh:mm");
} else {
Log.i("liyong", "====12非中国片区");
sdf = new SimpleDateFormat("h:MM a");
}
}
Log.i("liyong", "new Date(date)=" + new Date(date));
returnStr = sdf.format(new Date(date));
Log.i("liyong", "returnStr=" + returnStr);
//如果是在24 -- 48小时之内
} else if (absDuration > DAY_IN_MILLIS && absDuration <= TWODAYS_IN_MILLIS) {
//resId = com.android.internal.R.plurals.abbrev_num_days_ago;
//returnStr = r.getQuantityString(resId, (int) 1);
//如果是在48 - 一个星期
} else if (absDuration > TWODAYS_IN_MILLIS && absDuration <= WEEK_IN_MILLIS) {
sdf = new SimpleDateFormat("EEEE");
returnStr = sdf.format(new Date(date));
}
}
return returnStr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值