微信和qq时间格式模板实例详解
2019/6/27 21:41:45
本文主要是介绍微信和qq时间格式模板实例详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
直接上代码,代码中有注释,大家好好看!
/** * 将一个时间戳转换成提示性时间字符串,如 * 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; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
这篇关于微信和qq时间格式模板实例详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist
- 2024-03-11icons for vue
- 2024-03-07breadcrumbs react js
- 2024-03-06react login page example
- 2024-03-06react router uselocation
- 2024-03-04postgres jsonb_set