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
package com.tanpu.community.util;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class TimeUtils {
public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return instant.toEpochMilli();
}
public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
//计算迄今时间
public static String calUpToNowTime(LocalDateTime start) {
Duration between = Duration.between(start, LocalDateTime.now());
long duration = between.toMinutes();
if (duration < 1) {
return "刚刚";
} else if (duration < 60) {
return duration + "分钟前";
} else if (duration < 60 * 24) {
return duration / 60 + "小时前";
} else if (duration < 60 * 48) {
return "1天前";
} else if (duration < 60 * 24 * 180) {
return start.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
}
return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}
//格式化时间
public static String format(LocalDateTime start) {
return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}
public static LocalDateTime reFormat(String time) {
return LocalDateTime.parse(time,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
}
//计算迄今分钟
public static long calMinuteTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now());
return between.toMinutes();
}
//计算迄今毫秒数
public static long calMillisTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now());
return between.toMillis();
}
//计算迄今天数
public static long calDaysTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now());
return between.toDays();
}
//计算迄今小时数
public static long calHoursTillNow(LocalDateTime start) {
if (start==null){
return 0L;
}
Duration between = Duration.between(start, LocalDateTime.now());
return between.toHours();
}
//计算n天前的时间
public static LocalDateTime getDaysBefore(Integer number) {
return LocalDateTime.now().minusDays(number);
}
public static void main(String[] args) {
System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(180)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(1)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(18)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusHours(2)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusMinutes(12)));
System.out.println(calUpToNowTime(LocalDateTime.now().minusSeconds(12)));
}
public static boolean lessThan(String cacheTime, LocalDateTime updateTime) {
return calMillisTillNow(reFormat(cacheTime)) > calMillisTillNow(updateTime);
}
public static boolean lessThan(LocalDateTime before, LocalDateTime after) {
return calMillisTillNow(before) > calMillisTillNow(after);
}
}