TimeUtil.java
5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.naji.xiaotao.util;
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();
}
}