Java(仿QQ/微信聊天)的时间格式方法
2017-12-08 16:12
513 查看
前言
业务需求:需要根据时间戳或者日期,将时间显示的像QQ/微信聊天那样的格式。具体效果请自行查看QQ/微信。
此处给出实现的时间工具类
代码如下
import java.text.SimpleDateFormat; import java.util.Date; /** * Created by DeMon on 2017/12/8. */ 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(); } }
相关文章推荐
- Java各种获取系统当前时间方法和格式
- Java中常用的一种时间格式的转换方法
- Java开发中常见的时间格式转换方法
- java存储时间date到mysql的datetime格式的方法
- 微信和qq时间格式模板实例详解
- Java中时间戳转换成时间格式的方法
- java中获取不同时间格式的方法!及时间戳转化为时间格式
- JAVA、android常用时间处理方法及类似微信微博发表时间显示
- Java微信二次开发之10-解析接口中的消息创建时间CreateTime 并转换成需要的格式
- java中获取不同时间格式的方法
- 仿微信,QQ聊天页面时间戳的转换
- java中获取不同时间格式的方法
- Java各种获取系统当前时间方法 和 格式
- java时间生成格式解决方法
- java 小方法 判断字符串是否为时间格式,email格式,手机号格式
- Java各种获取系统当前时间方法 和 格式
- java利用时间格式生成唯一文件名的方法
- java中的时间计算和格式处理方法
- 微信、qq时间格式模板
- java部分基础类型及时间格式的数据格式相互转换方法整理