前言

业务需求:需要根据时间戳或者日期,将时间显示的像QQ/微信聊天那样的格式。
具体效果请自行查看QQ/微信。
此处给出实现的时间工具类

代码如下

import java.text.SimpleDateFormat;
import java.util.Date;public class TimeUtil {/*** 日期格式化** @return*/public static String DateformatTime(Date date) {//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//转换为日期long time = date.getTime();if (isThisYear(date)) {//今年SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");if (isToday(date)) { //今天int minute = minutesAgo(time);if (minute < 60) {//1小时之内if (minute <= 1) {//一分钟之内,显示刚刚return "刚刚";} else {return minute + "分钟前";}} else {return simpleDateFormat.format(date);}} else {if (isYestYesterday(date)) {//昨天,显示昨天return "昨天 " + simpleDateFormat.format(date);} else if (isThisWeek(date)) {//本周,显示周几String weekday = null;if (date.getDay() == 1) {weekday = "周一";}if (date.getDay() == 2) {weekday = "周二";}if (date.getDay() == 3) {weekday = "周三";}if (date.getDay() == 4) {weekday = "周四";}if (date.getDay() == 5) {weekday = "周五";}if (date.getDay() == 6) {weekday = "周六";}if (date.getDay() == 0) {weekday = "周日";}return weekday + " " + simpleDateFormat.format(date);} else {SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");return sdf.format(date);}}} else {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");return sdf.format(date);}}/*** String型时间戳格式化** @return*/public static String LongFormatTime(String time) {//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//转换为日期Date date = new Date();date.setTime(Long.parseLong(time));if (isThisYear(date)) {//今年SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");if (isToday(date)) { //今天int minute = minutesAgo(Long.parseLong(time));if (minute < 60) {//1小时之内if (minute <= 1) {//一分钟之内,显示刚刚return "刚刚";} else {return minute + "分钟前";}} else {return simpleDateFormat.format(date);}} else {if (isYestYesterday(date)) {//昨天,显示昨天return "昨天 " + simpleDateFormat.format(date);} else if (isThisWeek(date)) {//本周,显示周几String weekday = null;if (date.getDay() == 1) {weekday = "周一";}if (date.getDay() == 2) {weekday = "周二";}if (date.getDay() == 3) {weekday = "周三";}if (date.getDay() == 4) {weekday = "周四";}if (date.getDay() == 5) {weekday = "周五";}if (date.getDay() == 6) {weekday = "周六";}if (date.getDay() == 0) {weekday = "周日";}return weekday + " " + simpleDateFormat.format(date);} else {SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");return sdf.format(date);}}} else {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");return sdf.format(date);}}private static int minutesAgo(long time) {return (int) ((System.currentTimeMillis() - time) / (60000));}private static boolean isToday(Date date) {Date now = new Date();return (date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth()) && (date.getDate() == now.getDate());}private static boolean isYestYesterday(Date date) {Date now = new Date();return (date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth()) && (date.getDate() + 1 == now.getDate());}private static boolean isThisWeek(Date date) {Date now = new Date();if ((date.getYear() == now.getYear()) && (date.getMonth() == now.getMonth())) {if (now.getDay() - date.getDay() < now.getDay() && now.getDate() - date.getDate() > 0 && now.getDate() - date.getDate() < 7) {return true;}}return false;}private static boolean isThisYear(Date date) {Date now = new Date();return date.getYear() == now.getYear();}
}

本文转自https://blog.csdn.net/DeMonliuhui/article/details/78752734

java仿照微信聊天记录时间格式显示相关推荐

  1. 微信小程序IOS机型时间格式显示NaN异常问题

    new Date()转换时间时,IOS机型时间格式显示NaN 错误原因: ios不支持时间为2020-05-29这种格式的日期,必须转换为2020/05/29这种格式才能使用new Date()进行转 ...

  2. android 仿微信聊天时间格式化显示

    仿微信聊天时间格式化显示. 在同一年的显示规则: 如果是当天显示格式为 HH:mm 例:14:45 如果是昨天,显示格式为 昨天 HH:mm 例:昨天 13:12 如果是在同一周 显示格式为 周一 H ...

  3. 修改Windows10电脑日期时间格式显示星期

    鼠标右键[个性化],在弹出界面左键点击[任务栏],在"任务栏"界面找到[打开或关闭系统图标]按钮,在弹出的界面中即可控制任务栏部分图标的显示与否.以[时钟]为例,点如下图所示&qu ...

  4. ant design vue 表格中时间戳转换成时间格式显示

    ant design vue 表格中时间戳转换成时间格式显示 原始数据表格如上图,因为接口传递过来的时间是10位int类型的时间戳格式,所以前端需要我们把时间格式化. step1 安装moment n ...

  5. 小程序在安卓手机时间格式显示英文的坑

    今天说一个关于toLocaleDateString()的坑. 安卓手机picker 时间选择 默认时间显示英文 toLocaleDateString() 方法可根据本地时间把 Date 对象的日期部分 ...

  6. 微信小程序、苹果手机(ios)时间格式显示NAN.....(已解决)

    1.首先将这段代码复制到utils中 function formatTime(date) { var time = date==null?"": Date.parse(date.r ...

  7. java输出美国的时间_java显示当前美国洛杉矶时间

    java显示当前美国洛杉矶时间 复制代码 代码如下: SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss&quo ...

  8. 【Java语法】DateFormat时间格式转化、java.util.Date和java.sql.Date之间的相互转化

    使用DateFormat,将时间与日期相互转换 import java.text.DateFormat; import java.text.ParseException; import java.te ...

  9. 页面加载微信聊天记录图片不显示问题

    今天在做微信客服功能的时候页面通过异步请求微信的聊天记录,并把获取的结果appendchild页面中,发现微信的图片无权加载.如下图: 经过查阅资料得知,因为微信加载图片是通过一个地址请求然后返回的真 ...

最新文章

  1. boost之timer,progress_timer,progress_display的介绍及使用
  2. 测试邮件系统需要做的准备工作
  3. 实现div在固定区域跟随鼠标移动点击拖动而产生的变化
  4. su root 后还是不能使用useradd ,useradd 等命令
  5. 一文彻底弄懂 for forEach for-in for-of 的区别
  6. 磁盘阵列(RAID) 级别
  7. paip.模块间同步登录SSO实现总结
  8. 怎样在php中制作电子相册,如何制作纪念电子相册
  9. ARM 交叉编译器命名规则
  10. mPaaS 月度小报|魔方卡片(Cube)公测,十个卡片模板任意使用
  11. Java实现的中间库
  12. 圣多米尼克高中有计算机课吗,院校库_VPEA北美留学院校数据查询中心
  13. Kaggle Tweet Sentiment Extraction竞赛
  14. org.eclipse.core.runtime.CoreException: Plug-in org.eclipse.jdt.ui was unable to load class org.ecli
  15. 【AviUtl】动画效果,简易Glitch++(派生),学习笔记
  16. 前端表格里的数据不换行
  17. 【小迪安全】web安全|渗透测试|网络安全 | 学习笔记-7
  18. orcad DRC检查,元件C2都没连接,为什麼不报错
  19. 一款FPGA时序电路数据画图软件
  20. 魅族15系统是android,魅族工程师回复Flyme 8没有升级底层,魅族15还是安卓7.1

热门文章

  1. 大江大河2弹幕数据之词云分析、情感极性分析、主题分析、共现网络分析
  2. Mybatis异常_03_Invalid bound statement (not found)
  3. 105、基于51单片机的智能电子密码锁红外遥控控制系统设计
  4. Hudi学习02 -- Hudi核心概念
  5. windows下的免费免安装截图录屏录播工具
  6. unity3d 2D游戏中摄像机投影类型
  7. 智取天狼星计划(二)
  8. 番茄”为什么会凋谢?
  9. 汽车常识之动力系统简介
  10. 427 建立四叉树(递归、二维前缀和)