博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信、qq时间格式模板
阅读量:7220 次
发布时间:2019-06-29

本文共 7051 字,大约阅读时间需要 23 分钟。

  产品近来蛋疼,时间格式从做完到现在改了四遍了 ,最新的要求如下:

* 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

     这不是问题,很快写完代码。

     

1  /** 2      * 将一个时间戳转换成提示性时间字符串,如 3      * 2分钟内 无显示 4      * 2分钟-24小时 HH:mm 5      * 昨天 昨天 HH:mm 6      * 前天 前天 HH:mm 7      * 一年内 MM:DD HH:mm 8      * 去年 去年 MM:DD HH:mm 9      * 前年 前年 MM:DD HH:mm10      * 更远 yyyy:MM:DD HH:mm11      * 毫秒计算12      * @param charttime13      * @return14      */15     public static String convertChatDetailTimeFormat(long charttime) {16 17         long curTime = System.currentTimeMillis() ;18         long time = curTime - charttime;19 20         XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, time + "---时间差" + time/ 1000/ 60 + "分钟");21         XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, curTime + "---当前时间" + format(new Date(curTime), FORMAT_LONG_CN_1));22         XCApplication.base_log.i(XCConfig.TAG_SYSTEM_OUT, charttime + "---chartTime" + format(new Date(charttime), FORMAT_LONG_CN_1));23 24         if (time < 120 * 1000 && time >= 0) {25             return "刚刚";26         } else if (time >= 120 *1000 && time < 3600 * 24 * 1000) {27 28             return format(new Date(charttime), FORMAT_HH_MM);29 30         } else if (time >= 3600 * 24 * 1 * 1000 && time < 3600 * 24 * 2 * 1000) {31 32             return "昨天" + format(new Date(charttime), FORMAT_HH_MM);33 34         } else if (time >= 3600 * 24 * 2 * 1000 && time < 3600 * 24 * 3 * 1000) {35 36             return "前天" + format(new Date(charttime), FORMAT_HH_MM);37         } else if (time >= 3600 * 24 * 3 * 1000 && time < 3600 * 24 * 365 * 1 * 1000) {38 39             return format(new Date(charttime), FORMAT_MM_DD_HH_MM);40         } else if (time >= 3600 * 24 * 365 * 1 * 1000 && time < 3600 * 24 * 365 * 2 * 1000) {41 42             return "去年" + format(new Date(charttime), FORMAT_MM_DD_HH_MM);43         } else if (time >= 3600 * 24 * 365 * 2 * 1000 && time < 3600 * 24 * 365 * 3 * 1000) {44 45             return "前年" + format(new Date(charttime), FORMAT_MM_DD_HH_MM);46         } else if (time >= 3600 * 24 * 365 * 3 * 1000) {47 48             return format(new Date(charttime), FORMAT_LONG_CN_1);49         } else {50             return "刚刚";51         }52     }

     这里就有一个小问题,就是自然日时间跨越实际日时间,有可能出现昨天的时间不显示昨天,而显示为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 该显示为昨天还是去年。如果信息的接收时间比时间还要大,该如何显示。 经过一番撕逼,终于敲定,这里为了产品再次修改,要求产品立字据啊,作为终极版本存在。
1  /**  2      * 终极方法  3      * 将一个时间戳转换成提示性时间字符串,如  4      * 2分钟内 无显示  5      * 2分钟-今天  2分钟-今天 HH:mm  6      * 昨天 昨天 HH:mm  7      * 前天 前天 HH:mm  8      * 今年 MM:DD HH:mm  9      * 去年 去年 MM:DD HH:mm 10      * 前年 前年 MM:DD HH:mm 11      * 更远 yyyy:MM:DD HH:mm 12      * 毫秒计算 13      * @param time 14      * @return 15      */ 16     public static String convertWEChartTimeFormatFinalMethed(long time) { 17         long curTime = System.currentTimeMillis() ; 18         String showTimeFormat = ""; 19  20         long temp = curTime - time; 21         if (temp < 120 * 1000 && temp >= 0) { 22             showTimeFormat = ""; 23             return showTimeFormat; 24         } 25         Date mayTime = new Date(time); 26  27 //        Date today = UtilDate.parse("2015-01-01 02:02:02.001", UtilDate.FORMAT_FULL); 28         Date today = new Date(); 29         //时间值 30         String mayTime_FORMAT_SHORT = format(mayTime, FORMAT_SHORT); 31         String mayTime_FORMAT_SHORT_YEAR = getYear(mayTime); 32  33         if(mayTime.after(today)){ 34             //除此以外 35             showTimeFormat = format(mayTime, FORMAT_LONG_CN_1); 36  37         } else { 38             if(mayTime_FORMAT_SHORT != null && !mayTime_FORMAT_SHORT.trim().toString().equals("")){ 39                 //今天的时间yyyy-MM-dd 40                 String today_str = format(today, FORMAT_SHORT); 41                 String thisYear_str = getYear(today); 42  43                 //昨天的时间 yyyy-MM-dd 44                 Calendar calLastDay = Calendar.getInstance(); 45                 calLastDay.setTime(today); 46                 calLastDay.add(Calendar.DAY_OF_YEAR, -1); 47                 System.out.println("昨天:" + format(calLastDay.getTime(), FORMAT_SHORT)); 48                 String lastDay = format(calLastDay.getTime(), FORMAT_SHORT); 49  50                 //前天的时间 yyyy-MM-dd 51                 Calendar calPreviousDay = Calendar.getInstance(); 52                 calPreviousDay.setTime(today); 53                 calPreviousDay.add(Calendar.DAY_OF_YEAR, -2); 54                 System.out.println("前天:" + format(calPreviousDay.getTime(), FORMAT_SHORT)); 55                 String previousDay = format(calPreviousDay.getTime(), FORMAT_SHORT); 56  57                 //去年的时间 yyyy 58                 Calendar calLastYear = Calendar.getInstance(); 59                 calLastYear.setTime(today); 60                 calLastYear.add(Calendar.YEAR, -1); 61                 String lastYear = getYear(calLastYear.getTime()); 62                 System.out.println("去年:" + format(calLastYear.getTime(), FORMAT_SHORT)); 63  64                 //前年的时间 yyyy 65                 Calendar calPreviousYear = Calendar.getInstance(); 66                 calPreviousYear.setTime(today); 67                 calPreviousYear.add(Calendar.YEAR, -2); 68                 String previousYear = getYear(calPreviousYear.getTime()); 69                 System.out.println("前年:" + format(calPreviousYear.getTime(), FORMAT_SHORT)); 70  71                 //首先判断是否是今天 72                 if(mayTime_FORMAT_SHORT.equals(today_str)){ 73                     //今天,则显示为 13:12 74                     showTimeFormat = format(mayTime, FORMAT_HH_MM); 75                 } else if(mayTime_FORMAT_SHORT.equals(lastDay)){ 76                     //昨天 77                     showTimeFormat = "昨天 " + format(mayTime,FORMAT_HH_MM); 78  79                 } else if(mayTime_FORMAT_SHORT.equals(previousDay)){ 80                     //昨天 81                     showTimeFormat = "前天 " + format(mayTime,FORMAT_HH_MM); 82  83                 } else if(mayTime_FORMAT_SHORT_YEAR.equals(thisYear_str)){ 84                     //今年 85                     showTimeFormat = format(mayTime, FORMAT_MM_DD_HH_MM); 86                 } else if(mayTime_FORMAT_SHORT_YEAR.equals(lastYear)){ 87                     //去年 88                     showTimeFormat = "去年  " + format(mayTime, FORMAT_MM_DD_HH_MM); 89                 } else if(mayTime_FORMAT_SHORT_YEAR.equals(previousYear)){ 90                     //前年 91                     showTimeFormat = "前年  " + format(mayTime, FORMAT_MM_DD_HH_MM); 92                 } else { 93                     //除此以外 94                     showTimeFormat = format(mayTime, FORMAT_LONG_CN_1); 95                 } 96  97             } 98         } 99 100 101         return showTimeFormat;102     }

 

转载地址:http://kqhym.baihongyu.com/

你可能感兴趣的文章
ahjesus配置vsftpd虚拟用户在Ubuntu
查看>>
java的内存机制
查看>>
[摘录]第四部分 教训篇(2)
查看>>
gdb教程
查看>>
动态的加载类型
查看>>
36.scrapy框架采集全球玻璃网数据
查看>>
python matplotlib
查看>>
心灵指南 刘墉 第三辑 肯定自己 笔记
查看>>
centos7 tomcat nginx 动静分离显示权限不足
查看>>
弹珠游戏
查看>>
一切的原点
查看>>
OC内存管理
查看>>
洛谷P1801 黑匣子
查看>>
body和普通div背景图宽高百分比的区别
查看>>
【数字图像处理】OpenCV最大化HSV图像的"S"和"V"部分
查看>>
MongoDB的配置和使用
查看>>
我是如何在SQL Server中处理每天四亿三千万记录的
查看>>
python学习笔记(9)-python编程风格
查看>>
在VS和QT下配置pthread
查看>>
open
查看>>