Loading...

引言

记录使用

依赖

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.67</version>
</dependency>

代码

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
import org.apache.commons.lang3.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
* 时间管理工具类
*
* @author zhujx
*/
public class DateUtils {

public static final Long ONE_DAY = 24 * 60 * 60 * 1000L;

public static final Long ONE_HOUR = 60 * 60 * 1000L;

public static final Long ONE_MINUTE = 60 * 1000L;

public static final ThreadLocal<DateFormat> DF_YMD_INT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));

public static final ThreadLocal<DateFormat> DF_YMD = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

public static final ThreadLocal<DateFormat> DF_YMD_HMS = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

public static final ThreadLocal<DateFormat> DF_YMD_HMS_INT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

public static final ThreadLocal<DateFormat> DF_YMD_HMS_TZ = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));

public static final ThreadLocal<DateFormat> DF_YMD_HMS_S = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"));

public static final DateTimeFormatter COMMON_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

public static Date resetZero(Date date) {
try {
return DF_YMD.get().parse(DF_YMD.get().format(date));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

public static Date parse(String dateStr, String pattern) {
if (StringUtils.isBlank(dateStr) || StringUtils.isBlank(pattern)) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

public static String format(Date date, String pattern) {
if (date == null || StringUtils.isBlank(pattern)) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}

public static Date addDay(Date runDate, int i) {
return new Date(runDate.getTime() + ONE_DAY * i);
}

public static Date addHour(Date runDate, int i) {
return new Date(runDate.getTime() + ONE_HOUR * i);
}

public static boolean isBetween(Date time, Date startTime, Date endTime) {
if (time == null || startTime == null || endTime == null) {
return false;
}
return time.compareTo(startTime) > 0 && time.compareTo(endTime) < 0;
}

public static String dateYmdHmsString(Date date) {
if (date == null) {
return null;
}
return DF_YMD_HMS.get().format(date);
}

public static String dateYmdFormatString(Date date) {
if (date == null) {
return null;
}
return DF_YMD.get().format(date);
}

public static Long dateYmdLong(Date date) {
if (date == null) {
return null;
}
return Long.valueOf(DF_YMD_INT.get().format(date));
}

public static Long dateYmdHmsLong(Date date) {
if (date == null) {
return null;
}
return Long.valueOf(DF_YMD_HMS_INT.get().format(date));
}

}

如果你喜欢这篇文章,请点个赞,加个关注吧!!!