首页 > 其他 > 详细

处理时间显示:昨天,今天,刚刚,周几,或精准年月日

时间:2021-05-10 22:41:11      阅读:25      评论:0      收藏:0      [点我收藏+]
/**
* String型时间戳格式化
*
* @return
*/
public static String LongFormatTime(String time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = null;
try {
dt = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
time = String.valueOf(dt.getTime());
//转换为日期
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 simpleDateFormat.format(date);
}
} else {
return simpleDateFormat.format(date);
}
} else {
if (isYestYesterday(date)) {//昨天,显示昨天
return "昨天 ";
} 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;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);

 

处理时间显示:昨天,今天,刚刚,周几,或精准年月日

原文:https://www.cnblogs.com/duno/p/14752661.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!