直接上代码,代码中有注释,大家好好看!
/**
* 将一个时间戳转换成提示性时间字符串,如
* 2分钟内 无显示
* 2分钟-24小时 hh:mm
* 昨天 昨天 hh:mm
* 前天 前天 hh:mm
* 一年内 mm:dd hh:mm
* 去年 去年 mm:dd hh:mm
* 前年 前年 mm:dd hh:mm
* 更远 yyyy:mm:dd hh:mm
* 毫秒计算
* @param charttime
* @return
*/
public static string convertchatdetailtimeformat(long charttime) {
long curtime = system.currenttimemillis() ;
long time = curtime - charttime;
xcapplication.base_log.i(xcconfig.tag_system_out, time + "---时间差" + time/ 1000/ 60 + "分钟");
xcapplication.base_log.i(xcconfig.tag_system_out, curtime + "---当前时间" + format(new date(curtime), format_long_cn_1));
xcapplication.base_log.i(xcconfig.tag_system_out, charttime + "---charttime" + format(new date(charttime), format_long_cn_1));
if (time < 120 * 1000 && time >= 0) {
return "刚刚";
} else if (time >= 120 *1000 && time < 3600 * 24 * 1000) {
return format(new date(charttime), format_hh_mm);
} else if (time >= 3600 * 24 * 1 * 1000 && time < 3600 * 24 * 2 * 1000) {
return "昨天" + format(new date(charttime), format_hh_mm);
} else if (time >= 3600 * 24 * 2 * 1000 && time < 3600 * 24 * 3 * 1000) {
return "前天" + format(new date(charttime), format_hh_mm);
} else if (time >= 3600 * 24 * 3 * 1000 && time < 3600 * 24 * 365 * 1 * 1000) {
return format(new date(charttime), format_mm_dd_hh_mm);
} else if (time >= 3600 * 24 * 365 * 1 * 1000 && time < 3600 * 24 * 365 * 2 * 1000) {
return "去年" + format(new date(charttime), format_mm_dd_hh_mm);
} else if (time >= 3600 * 24 * 365 * 2 * 1000 && time < 3600 * 24 * 365 * 3 * 1000) {
return "前年" + format(new date(charttime), format_mm_dd_hh_mm);
} else if (time >= 3600 * 24 * 365 * 3 * 1000) {
return format(new date(charttime), format_long_cn_1);
} else {
return "刚刚";
}
}
这里就有一个小问题,就是自然日时间跨越实际日时间,有可能出现昨天的时间不显示昨天,而显示为hh:mm,于是测试找上门来,要求改,将2分钟-24小时的条件改为2分钟-今日内。
那么这里的需求就改为
* 2分钟内 无显示
* 2分钟-今日 hh:mm
* 昨天 昨天 hh:mm
* 前天 前天 hh:mm
* 今年 mm:dd hh:mm
* 去年 去年 mm:dd hh:mm
* 前年 前年 mm:dd hh:mm
* 更远 yyyy:mm:dd hh:mm
这也不是多大的问题,问题是在跨年的情况该如何,2015-01-01 00:01.001 的前三分钟接受的消息,也就是2014-12-31 该显示为昨天还是去年。如果信息的接收时间比时间还要大,该如何显示。
经过一番撕逼,终于敲定,这里为了产品再次修改,要求产品立字据啊,作为终极版本存在。
/**
* 终极方法
* 将一个时间戳转换成提示性时间字符串,如
* 2分钟内 无显示
* 2分钟-今天 2分钟-今天 hh:mm
* 昨天 昨天 hh:mm
* 前天 前天 hh:mm
* 今年 mm:dd hh:mm
* 去年 去年 mm:dd hh:mm
* 前年 前年 mm:dd hh:mm
* 更远 yyyy:mm:dd hh:mm
* 毫秒计算
* @param time
* @return
*/
public static string convertwecharttimeformatfinalmethed(long time) {
long curtime = system.currenttimemillis() ;
string showtimeformat = "";
long temp = curtime - time;
if (temp < 120 * 1000 && temp >= 0) {
showtimeformat = "";
return showtimeformat;
}
date maytime = new date(time);
// date today = utildate.parse("2015-01-01 02:02:02.001", utildate.format_full);
date today = new date();
//时间值
string maytime_format_short = format(maytime, format_short);
string maytime_format_short_year = getyear(maytime);
if(maytime.after(today)){
//除此以外
showtimeformat = format(maytime, format_long_cn_1);
} else {
if(maytime_format_short != null && !maytime_format_short.trim().tostring().equals("")){
//今天的时间yyyy-mm-dd
string today_str = format(today, format_short);
string thisyear_str = getyear(today);
//昨天的时间 yyyy-mm-dd
calendar callastday = calendar.getinstance();
callastday.settime(today);
callastday.add(calendar.day_of_year, -1);
system.out.println("昨天:" + format(callastday.gettime(), format_short));
string lastday = format(callastday.gettime(), format_short);
//前天的时间 yyyy-mm-dd
calendar calpreviousday = calendar.getinstance();
calpreviousday.settime(today);
calpreviousday.add(calendar.day_of_year, -2);
system.out.println("前天:" + format(calpreviousday.gettime(), format_short));
string previousday = format(calpreviousday.gettime(), format_short);
//去年的时间 yyyy
calendar callastyear = calendar.getinstance();
callastyear.settime(today);
callastyear.add(calendar.year, -1);
string lastyear = getyear(callastyear.gettime());
system.out.println("去年:" + format(callastyear.gettime(), format_short));
//前年的时间 yyyy
calendar calpreviousyear = calendar.getinstance();
calpreviousyear.settime(today);
calpreviousyear.add(calendar.year, -2);
string previousyear = getyear(calpreviousyear.gettime());
system.out.println("前年:" + format(calpreviousyear.gettime(), format_short));
//首先判断是否是今天
if(maytime_format_short.equals(today_str)){
//今天,则显示为 13:12
showtimeformat = format(maytime, format_hh_mm);
} else if(maytime_format_short.equals(lastday)){
//昨天
showtimeformat = "昨天 " + format(maytime,format_hh_mm);
} else if(maytime_format_short.equals(previousday)){
//昨天
showtimeformat = "前天 " + format(maytime,format_hh_mm);
} else if(maytime_format_short_year.equals(thisyear_str)){
//今年
showtimeformat = format(maytime, format_mm_dd_hh_mm);
} else if(maytime_format_short_year.equals(lastyear)){
//去年
showtimeformat = "去年 " + format(maytime, format_mm_dd_hh_mm);
} else if(maytime_format_short_year.equals(previousyear)){
//前年
showtimeformat = "前年 " + format(maytime, format_mm_dd_hh_mm);
} else {
//除此以外
showtimeformat = format(maytime, format_long_cn_1);
}
}
}
return showtimeformat;
}