日期时间工具类

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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package msdev.test.util;

import org.apache.commons.lang.time.DateFormatUtils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 日期时间工具类
*/
public class DateTimeUtil {

/**
* 《java格式化字母表》
* <p>
* Symbol Meaning Presentation Example
* G era designator Text AD
* y year Number 2009
* M month in year Text & Number July & 07
* d day in month Number 10
* h hour in am/pm (1-12) Number 12
* H hour in day (0-23) Number 0
* m minute in hour Number 30
* s second in minute Number 55
* S millisecond Number 978
* E day in week Text Tuesday
* D day in year Number 189
* F day of week in month Number 2 (2nd Wed in July)
* w week in year Number 27
* W week in month Number 2
* a am/pm marker Text PM
* k hour in day (1-24) Number 24
* K hour in am/pm (0-11) Number 0
* z time zone Text Pacific Standard Time
* ' escape for text Delimiter (none)
* ' single quote Literal '
* ————————————————
*
* @param args
*/

public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
System.out.println(LocalDate.now().atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli());

/**
* 获取当前日期:方式三
* now():获取当前的日期、时间、日期+时间
*/
//结果:2022-08-01
LocalDate localDate = LocalDate.now();
//结果:15:08:35.993
LocalTime localTime = LocalTime.now();
//结果:2022-08-01T15:08:35.994
LocalDateTime localDateTime = LocalDateTime.now();
//获取前一天时间:2022-07-31
LocalDate yesterday = LocalDate.now().minusDays(1);

System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
System.out.println(yesterday);


/**
* 获取当前日期:方式四
*/
//只有日期: 例如:2022-08-01
java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
//只有时间: 例如:15:05:52
java.sql.Time time = new java.sql.Time(System.currentTimeMillis());
System.out.println(date);
System.out.println(time);


/**
* 获取当前日期:方式五
* 结果:2022-09-19 14:57:52.543
*/
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(timestamp);

}


/**
* 计算两个日期时间之间的时间差,转为毫秒数
* 注意:计算日期时间,必须是精确到秒级别,才能参与运算,不然会报错
*
* @param startDate 开始日期
* @param endDate 结束日期
* @return
* @author LiuMingFu
* @date 2023-05-17
*/
public long getDurationMillis(LocalDateTime startDate, LocalDateTime endDate) {
//获取两个日期时间之间的相差日期对象
Duration duration = Duration.between(startDate, endDate);
// Duration between = Duration.between(LocalDate.now(), LocalDate.now());
//转为纳秒数
long l = duration.toNanos();
//转为毫秒数
long millis = duration.toMillis();
//转为分钟数
long minutes = duration.toMinutes();
//转为小时数
long hours = duration.toHours();
//转为天数
long days = duration.toDays();
return millis;
}


/**
* 返回当前时间戳:方式一
*
* @return
*/
public static long getCurrentTimeMillisBySystem() {
//利用虚拟机对象直接获取时间戳
long timeMillis = System.currentTimeMillis();
return timeMillis;
}


/**
* 返回当前时间戳:方式二
*
* @return
*/
public static long getCurrentTimeMillisByDate() {
//利用日期类获取时间戳
Date date = new Date();
long time = date.getTime();
return time;
}


/**
* 根据LocalDateTime类型,获取当前时间戳
*
* @param
* @return
* @author LiuMingFu
* @date 2023/3/23
*/
public static long getCurrentTimeMillisByLocalDateTime() {
//默认东八区:北京时间
return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
}


/**
* 根据LocalDate类型,获取当前时间戳
*
* @param
* @return
* @author LiuMingFu
* @date 2023/3/23
*/
public static long getCurrentTimeMillisByLocalDate() {
//默认东八区:北京时间,yyyy-MM-dd 00:00:00
return LocalDate.now().atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli();
}


/**
* 可以将时间戳转为自定义格式的日期形式:方式一
*
* @return
*/
public static String getFormatDate() {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd GGG HH:mm:ss aaa");
//方式一:传入日期对象
String formatDate = simpleDateFormat.format(date);

//方式二:传入时间戳
String formatDate2 = simpleDateFormat.format(date.getTime());
return formatDate;
}


/**
* 可以将时间戳转为自定义格式的日期形式:方式二
* 注意:需要导入Apache下面的一个包
*
* @return
*/
public static String getFormatDate_2() {
Date date = new Date();
String format = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss.SSSSSS");
return format;
}


/**
* 将时间戳转换为LocalDate对象
*
* @param timestamp 时间戳
* @return
* @author LiuMingFu
* @date 2023-05-17
*/
public static LocalDate getLocalDateByTimestamp(long timestamp) {
//将时间戳转为瞬时对象
Instant instant = Instant.ofEpochMilli(timestamp);
//设置系统默认时区
LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
return localDate;
}


/**
* 将时间戳转换为LocalDateTime对象
*
* @param timestamp 时间戳
* @return
* @author LiuMingFu
* @date 2023-05-17
*/
public LocalDateTime getLocalDateTimeByTimestamp(long timestamp) {
//将时间戳转为瞬时对象
Instant instant = Instant.ofEpochMilli(timestamp);
//设置系统默认时区
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
return localDateTime;
}


/**
* 将时间戳转换为LocalTime对象
*
* @param timestamp 时间戳
* @return
* @author LiuMingFu
* @date 2023-05-17
*/
public LocalTime getLocalTimeByTimestamp(long timestamp) {
//将时间戳转为瞬时对象
Instant instant = Instant.ofEpochMilli(timestamp);
//设置系统默认时区
LocalTime localTime = instant.atZone(ZoneId.systemDefault()).toLocalTime();
return localTime;
}


/**
* 将时间日期字符串转为LocalDateTime类型对象
*
* @param dateTime 时间日期字符串,例如:2023-12-23 23:56:55
* @return
* @author LiuMingFu
* @date 2023/3/2
*/
public static LocalDateTime getLocalDateTime(String dateTime) {
//解析时间日期的模式
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(dateTime, pattern);
}

/**
* 将日期字符串转为LocalDate类型对象
*
* @param date 日期字符串,例如:2023-12-23
* @return
* @author LiuMingFu
* @date 2023/3/2
*/
public static LocalDate getLocalDate(String date) {
//解析日期的模式
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return LocalDate.parse(date, pattern);
}


/**
* 将时间字符串转为LocalTime类型对象
*
* @param time 时间字符串,例如:23:56:55
* @return
* @author LiuMingFu
* @date 2023/3/2
*/
public static LocalTime getLocalTime(String time) {
//解析时间的模式
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("HH:mm:ss");
return LocalTime.parse(time, pattern);
}

/** 计算两个日期之间的间隔天数
*
* @param startDateParam
* @param endDateParam
* @return
*/
public static int getAllDateCount(LocalDate startDateParam, LocalDate endDateParam) {
// 声明保存日期集合
List<LocalDate> list = new ArrayList<>();
while (!startDateParam.isAfter(endDateParam)) {
list.add(startDateParam);
startDateParam = startDateParam.plusDays(1);
}
if (list.size() <= 1) {
return 0;
}
return list.size() - 1;
}

/** 计算两个日期之间的间隔毫秒数
*
* @param startDate
* @param endDate
* @return
*/
public static long getDurationInSeconds(LocalDateTime startDate, LocalDateTime endDate) {
Duration difference = Duration.between(startDate, endDate);
return difference.getSeconds();
}
}