Java仿微信时间显示
Java仿微信时间显示
- 需求
- 代码实现
- 1.dto类设计
- 2.controller层
- 3.service层
- 4.时间格式化(service层)
- 4.分组函数(service层)
- 总结
需求
微信聊天消息时间显示说明
1、当天的消息,以每5分钟为一个跨度的显示时间;
2、消息超过1天、小于1周,显示星期+收发消息的时间;
3、消息大于1周,显示手机收发时间的日期。
考虑了一下,需求变更如下:
- 当天跨度为5分钟
- 当天之前的跨度统一10分钟
效果类似下图:
代码实现
1.dto类设计
数据库实际仅用一个字段存储时间:
LocalDateTime createTime
为了便于之后的跨度计算加上几个辅助字段,这些字段不存入数据库,所以只写在dto类中,而不是entity类中。
相关字段如下
//消息实际发送时间private LocalDateTime createTime;//格式化时间private String dealTime;//用于格式化时间的分组// 5:今天 4:昨天 3:一周以内 2:月 1:年private Byte flagTime;//消息间每隔一个时间跨度存放一次,也是最后要取的时间值private String resultTime;
2.controller层
public PageResponse<PrivateMessageDTO> getDetailPrivateLetterByTime(@RequestBody @Validated PageQuery<PrivateMessageDTO> request) {//此处用户获取用户的消息记录PageResponse<PrivateMessageDTO> page = privateMessageService.getDetailPrivateLetter(request);//对消息记录的时间进行处理return privateMessageService.getDetailPrivateLetterByTime(page);}
3.service层
// 时间处理函数@Overridepublic PageResponse<PrivateMessageDTO> getDetailPrivateLetterByTime(PageResponse<PrivateMessageDTO> request) {List<PrivateMessageDTO> list = request.getListData();for(PrivateMessageDTO temp:list){//时间格式化处理,如 将2000-01-01 12:00:00 转换成 年月日的格式//getTimeString 时间格式化函数,之后会给出代码String result = getTimeString(temp.getCreateTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());//对时间进行标记,为消息做一个归类//比如 消息发送时间在昨天以前,又在一周以内的归到一组中// 5:今天 4:昨天 3:一周以内 2:月 1:年if(result.contains("年")){temp.setFlagTime((byte) 1);}else if(result.contains("月")){temp.setFlagTime((byte) 2);}else if(result.contains("星期")){temp.setFlagTime((byte) 3);}else if(result.contains("昨天")){temp.setFlagTime((byte) 4);}else{temp.setFlagTime((byte) 5);}temp.setDealTime(result);}//分组操作:开始分组,按时间跨度设置resultTime的值list = dealTime(list);request.setListData(list);return request;}
4.时间格式化(service层)
负责对时间进行格式化的操作,这里我借用了某位大佬的代码,文末会给出这位大佬的博文链接,这里先表示一下感谢。
public static String getTimeString(Long timestamp) {String result = "";String weekNames[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};String hourTimeFormat = "HH:mm";String monthTimeFormat = "M月d日 HH:mm";String yearTimeFormat = "yyyy年M月d日 HH:mm";try {Calendar todayCalendar = Calendar.getInstance();Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(timestamp);if (todayCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {//当年if (todayCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)) {//当月int temp = todayCalendar.get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH);switch (temp) {case 0://今天result = getTime(timestamp, hourTimeFormat);break;case 1://昨天result = "昨天 " + getTime(timestamp, hourTimeFormat);break;case 2:case 3:case 4:case 5:case 6:int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);result = weekNames[dayOfWeek - 1] + " " + getTime(timestamp, hourTimeFormat);break;default:result = getTime(timestamp, monthTimeFormat);break;}} else {result = getTime(timestamp, monthTimeFormat);}} else {result = getTime(timestamp, yearTimeFormat);}return result;} catch (Exception e) {log.error("getTimeString", e.getMessage());return "";}}public static String getTime(long time, String pattern) {Date date = new Date(time);return dateFormat(date, pattern);}public static String dateFormat(Date date, String pattern) {SimpleDateFormat format = new SimpleDateFormat(pattern);return format.format(date);}
4.分组函数(service层)
对时间按跨度进行分组,假如两个消息发送的时间在5分钟以内,它们将会被归到一组,该组第一条消息的resultTime会被赋值(值为该消息的发送时间),该组第二条,第三条的resultTime将不会被赋值。前台只会取resultTime的值进行显示。
List<PrivateMessageDTO> dealTime(List<PrivateMessageDTO> list){//按标记进行初步分组Map<Byte,List<PrivateMessageDTO>> map = list.stream().collect(Collectors.groupingBy(PrivateMessageDTO::getFlagTime));List<PrivateMessageDTO> newList = new ArrayList<>();Duration duration;for (Map.Entry<Byte,List<PrivateMessageDTO>> entry : map.entrySet()) {//如果是当天的消息每隔五分钟统计一次if(entry.getKey()==5){List<PrivateMessageDTO> tempList = entry.getValue();LocalDateTime start = LocalDateTime.now();for(int i=0;i<tempList.size();i++){//计算间隔时间duration = Duration.between(start,tempList.get(i).getCreateTime());//如果大于五分钟记录一次,并获取新的比较值if(duration.toMinutes()>5){start = tempList.get(i).getCreateTime();tempList.get(i).setResultTime(tempList.get(i).getDealTime());}}}else if(entry.getKey()==4 || entry.getKey()==3 || entry.getKey()==2 || entry.getKey()==1){//如果是当天之前每隔十分钟统计一次List<PrivateMessageDTO> tempList = entry.getValue();LocalDateTime start = LocalDateTime.now();for(int i=0;i<tempList.size();i++){duration = Duration.between(start,tempList.get(i).getCreateTime());if(Math.abs(duration.toMinutes())>10){start = tempList.get(i).getCreateTime();tempList.get(i).setResultTime(tempList.get(i).getDealTime());}}}newList.addAll(tempList);}return newList;}
总结
代码大致如上,感兴趣的可以代我验证一下,逻辑也许会有点不严谨的地方,代码应该也有可优化的地方。恳请各位大佬赐教。
最后,感谢提供 时间格式化 部分代码的大佬,该部分博文链接如下:
。。。。由于先前清了浏览记录,刚才去查了一下,发现找不到这部分的博文地址,如果有知道的朋友给个链接。::>_<::
Java仿微信时间显示相关推荐
- Android/java 仿微信聊天列表时间显示规则
微信时间显示规则: 今天: HH:mm ,例 8:28 昨天: 昨天 HH:mm, 例 昨天 9:27 近7天 : 星期X HH:mm ,例 星期一 6:25 今年: M月d日 HH:mm 例 3月2 ...
- 安卓显示时间java,Android/java 仿微信聊天列表时间显示规则
往年: yyyy年M月d日 HH:mm 例 2018年6月9日 6:52 public static String getTimeString(Long timestamp) { String res ...
- Java 正则表达式格式化时间显示
/* * test.java * Version 1.0.0 * Created on 2017年12月16日 * Copyright ReYo.Cn */ package reyo.sdk.util ...
- ios系统 微信时间显示NANANANA
问题: 在html页面中获得后台传过来的一个时间并显示在页面上,我用getFullYear() ,getMonth(),getDate()分别获得了年月日在电脑上和三星手机上页面都能正确的显示时间,而 ...
- java工具类-java仿微信九宫格头像
创建Utils类 ImageUtil package com.mrd.utils;import javax.imageio.ImageIO; import java.awt.Color; import ...
- java仿微信登录界面_android 界面设计潮流:仿微信5.2界面源码
package com.example.isweixin; //Download by 链接已屏蔽 import java.util.Timer; import java.util.TimerTask ...
- 仿微信评论显示更多与收起
下载地址 http://download.csdn.net/detail/sinat_28238111/9660877
- js封装毫秒时间戳转换仿微信聊天时间显示格式
js封装毫秒时间戳转换仿微信聊天时间显示格式 先把微信的时间显示规则拍上来 微信聊天消息时间显示说明 1.当天的消息,以每5分钟为一个跨度的显示时间: 2.消息超过1天.小于1周,显示星期+收发消息的 ...
- java 友好时间显示_仿微信的IM聊天时间显示格式(含iOS/Android/Web实现)[图文+源码]...
本文为原创分享,转载请注明出处. 1.引言 即时通讯IM应用中的聊天消息时间显示是个再常见不过的需求,现在都讲究用户体验,所以时间显示再也不能像传统软件一样简单粗地暴显示成"年/月/日 时: ...
最新文章
- const的用法,特别是用在函数后面
- 准备搭建经营分析前端试验型平台
- 使用DocFX生成文档
- java sql函数_Java调用Sql存储过程实例讲解
- 定时器 槽函数没执行_Web服务器项目详解 07 定时器处理非活动连接(上)
- 将结构体数据存储到一段字符串string中
- 用Python3.6操作HBase之HBase-Thrift
- 【Assertion failed (blockSize % 2 == 1 blockSize > 1) in cv::adaptiveThreshold】
- c++操作打印机那些事
- 技嘉主板bios设置方法
- CRT使用(二)CRT软件修改超时时间
- SFDC中的DEBUG
- Ceph管理平台Calamari的架构与功能分析
- 石化行业工作调度,如何选择合适的防爆对讲机?
- Banner——轮播图
- 一个故事讲完进程、线程和协程
- Hadoop-HDFS
- 真正的宇宙中心?未来科技城、云城或将彻底爆发。逃离深圳,拥抱杭州的启示
- 【Kubernetes 系列】Kubernetes 创建K8s集群项目
- 车站计算机的运行方式有,列车编队运行方式及控制研究
热门文章
- Android拖拽排序控件DragGridView
- js+html2canvas实现网页放大镜效果:放大镜图片使用css样式background背景图,鼠标移动使用样式background-position动态设置
- linux 常用软件2
- 错误代码:0x800704cf 不能访问网络位置(win7 连不上smb了)
- 工行科技精英岗和建行笔试和面试都已经通过,把自己的复习资料分享给大家。
- 解决MySQL_python-1.2.5-cp27-none-win_amd64.whl is not a supported wheel on this platform.(win10)
- pkill 命令_pkill和pgrep:流程管理命令
- layui下layer弹出框(iframe)
- 常用对象操作:(4)
- Nancy 入门教程