1
|
package com.tianbo.util.Date;
|
1
|
package com.tianbo.util.Date;
|
2
|
|
2
|
|
|
|
3
|
+import java.text.ParseException;
|
3
|
import java.text.SimpleDateFormat;
|
4
|
import java.text.SimpleDateFormat;
|
4
|
-import java.time.LocalDateTime;
|
|
|
5
|
-import java.time.ZoneId;
|
5
|
+import java.time.*;
|
6
|
import java.time.format.DateTimeFormatter;
|
6
|
import java.time.format.DateTimeFormatter;
|
7
|
import java.time.format.DateTimeParseException;
|
7
|
import java.time.format.DateTimeParseException;
|
|
|
8
|
+import java.util.Calendar;
|
8
|
import java.util.Date;
|
9
|
import java.util.Date;
|
9
|
|
10
|
|
|
|
11
|
+
|
|
|
12
|
+/**
|
|
|
13
|
+ *
|
|
|
14
|
+ * 日期工具类
|
|
|
15
|
+ * @since 1.0
|
|
|
16
|
+ * @date 2019-04-21
|
|
|
17
|
+ * @author mrz
|
|
|
18
|
+ *
|
|
|
19
|
+ */
|
10
|
public final class DateUtil {
|
20
|
public final class DateUtil {
|
|
|
21
|
+
|
|
|
22
|
+ public static String F19 = "yyyy-MM-dd HH:mm:ss";
|
|
|
23
|
+
|
|
|
24
|
+ public static String F14 = "yyyyMMddHHmmss";
|
|
|
25
|
+
|
|
|
26
|
+ public static String F10 = "yyyy-MM-dd";
|
|
|
27
|
+
|
|
|
28
|
+ public static String F8 = "yyyyMMdd";
|
|
|
29
|
+
|
|
|
30
|
+ public static String[] dataStringFormats = {F8, F10, F14, F19, "yyyy/MM/dd", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH:mm:ss", "yyyy-MM-dd HH:mm"};
|
|
|
31
|
+
|
11
|
private static Date currentDate = new Date();
|
32
|
private static Date currentDate = new Date();
|
12
|
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
33
|
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
34
|
+ private static SimpleDateFormat sdf_yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
|
13
|
private static SimpleDateFormat timesdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
35
|
private static SimpleDateFormat timesdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
14
|
|
36
|
|
15
|
public static String getToday(){
|
37
|
public static String getToday(){
|
16
|
return sdf.format(currentDate);
|
38
|
return sdf.format(currentDate);
|
17
|
}
|
39
|
}
|
|
|
40
|
+
|
|
|
41
|
+ public static String getTodayBy_yyyyMMdd(){
|
|
|
42
|
+ return sdf_yyyyMMdd.format(currentDate);
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
|
45
|
+ public static Date formatByyyyyMMdd(String dateStr){
|
|
|
46
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
47
|
+ LocalDate beginDateTime = LocalDate.parse(dateStr, formatter);
|
|
|
48
|
+ Date date = localDateToDate(beginDateTime);;
|
|
|
49
|
+ return date;
|
|
|
50
|
+ }
|
|
|
51
|
+
|
18
|
public static String getDDTM(){
|
52
|
public static String getDDTM(){
|
19
|
return timesdf.format(currentDate);
|
53
|
return timesdf.format(currentDate);
|
20
|
}
|
54
|
}
|
|
@@ -34,4 +68,337 @@ public final class DateUtil { |
|
@@ -34,4 +68,337 @@ public final class DateUtil { |
34
|
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
|
68
|
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
|
35
|
}
|
69
|
}
|
36
|
|
70
|
|
|
|
71
|
+ /**
|
|
|
72
|
+ * 将字符串转换为long类型的值(不包含-符号)
|
|
|
73
|
+ * @param dateString 2016-10-12
|
|
|
74
|
+ * @return 20161012
|
|
|
75
|
+ */
|
|
|
76
|
+ public static long stringToDateLong(String dateString) {
|
|
|
77
|
+ String[] dates = dateString.split("-");
|
|
|
78
|
+ return Long.valueOf(dates[0] + dates[1] + dates[2]);
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ /**
|
|
|
82
|
+ * 将日期转化为默认的格式显示
|
|
|
83
|
+ * @param date Date实例
|
|
|
84
|
+ * @return 2017-06-06
|
|
|
85
|
+ */
|
|
|
86
|
+ public static String dateToString(Date date) {
|
|
|
87
|
+ return dateToString(date, F10);
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ /**
|
|
|
91
|
+ * @param date Date实例
|
|
|
92
|
+ * @param format yyyy-MM-dd
|
|
|
93
|
+ * @return 2017-06-06
|
|
|
94
|
+ */
|
|
|
95
|
+ public static String dateToString(Date date, String format) {
|
|
|
96
|
+ if (date == null) {
|
|
|
97
|
+ return null;
|
|
|
98
|
+ }
|
|
|
99
|
+ SimpleDateFormat sf = new SimpleDateFormat(format);
|
|
|
100
|
+ return sf.format(date);
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ /**
|
|
|
104
|
+ * 时间戳转默认字符串日期
|
|
|
105
|
+ * @param time 1496739850253
|
|
|
106
|
+ * @return 2017-06-06
|
|
|
107
|
+ */
|
|
|
108
|
+ public static String longToString(long time) {
|
|
|
109
|
+ return longToString(time, F10);
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ /**
|
|
|
113
|
+ * 时间戳转字符串日期(格式可以自己选择)
|
|
|
114
|
+ * @param time 1496739850253
|
|
|
115
|
+ * @param format yyyy-MM-dd
|
|
|
116
|
+ * @return 2017-06-06
|
|
|
117
|
+ */
|
|
|
118
|
+ public static String longToString(long time, String format) {
|
|
|
119
|
+ SimpleDateFormat sf = new SimpleDateFormat(format);
|
|
|
120
|
+ return sf.format(new Date(time));
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ /**
|
|
|
124
|
+ * 在指定日期上加上一定天数获得新的日期
|
|
|
125
|
+ * @param day 2016-06-06
|
|
|
126
|
+ * @param addDay 10
|
|
|
127
|
+ * @return 2016-06-16
|
|
|
128
|
+ */
|
|
|
129
|
+ public static String getNextDay(String day, int addDay) {
|
|
|
130
|
+ Calendar calendar = getCalendar(day);
|
|
|
131
|
+ calendar.add(Calendar.DAY_OF_MONTH, addDay);
|
|
|
132
|
+ return getDateString(calendar);
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ /**
|
|
|
136
|
+ * 获取当前时间(包含小时、分、秒)
|
|
|
137
|
+ * @return 2016-06-06 10:20:10
|
|
|
138
|
+ */
|
|
|
139
|
+ public static String getCurrTime() {
|
|
|
140
|
+ return dateToString(new Date(), F19);
|
|
|
141
|
+ }
|
|
|
142
|
+
|
|
|
143
|
+ /**
|
|
|
144
|
+ * 获取当前日期(不包含小时、分、秒)
|
|
|
145
|
+ * @return 2016-06-06
|
|
|
146
|
+ */
|
|
|
147
|
+ public static String getCurrDate() {
|
|
|
148
|
+ return dateToString(new Date(), F10);
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ /**
|
|
|
152
|
+ * 获取日期相距天数
|
|
|
153
|
+ * @param startDate 2016-06-06
|
|
|
154
|
+ * @param endDate 2016-06-10
|
|
|
155
|
+ * @return int 4
|
|
|
156
|
+ */
|
|
|
157
|
+ public static int getCompareDate(String startDate, String endDate) {
|
|
|
158
|
+ try {
|
|
|
159
|
+ SimpleDateFormat formatter = new SimpleDateFormat(F10);
|
|
|
160
|
+ Date date1 = formatter.parse(startDate);
|
|
|
161
|
+ Date date2 = formatter.parse(endDate);
|
|
|
162
|
+ long l = date2.getTime() - date1.getTime() + 1000;
|
|
|
163
|
+ long d = l / (24 * 60 * 60 * 1000);
|
|
|
164
|
+ return (int) d;
|
|
|
165
|
+ } catch (ParseException e) {
|
|
|
166
|
+ }
|
|
|
167
|
+ return 0;
|
|
|
168
|
+ }
|
|
|
169
|
+
|
|
|
170
|
+ private static Calendar getCalendar(String day) {
|
|
|
171
|
+ Calendar cal = Calendar.getInstance();
|
|
|
172
|
+ cal.set(Integer.parseInt(day.substring(0, 4)), Integer.parseInt(day.substring(5, 7)) - 1, Integer.parseInt(day.substring(8, 10)), 0, 0, 0);
|
|
|
173
|
+ return cal;
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+ /**
|
|
|
177
|
+ * 当前时间几小时相差多少时间
|
|
|
178
|
+ * @param hour
|
|
|
179
|
+ * @return
|
|
|
180
|
+ * @since 2017年8月18日
|
|
|
181
|
+ */
|
|
|
182
|
+ public static Date getAddHourTime(int hour) {
|
|
|
183
|
+ Calendar dalendar = Calendar.getInstance();
|
|
|
184
|
+ dalendar.add(Calendar.HOUR_OF_DAY, hour);
|
|
|
185
|
+ return dalendar.getTime();
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
|
188
|
+ /**
|
|
|
189
|
+ * 将Calendar转换为日期字符串(字符串的格式:2018-04-21)
|
|
|
190
|
+ * @param cale
|
|
|
191
|
+ * @return
|
|
|
192
|
+ */
|
|
|
193
|
+ public static String getDateString(Calendar cale) {
|
|
|
194
|
+ int year = cale.get(Calendar.YEAR);
|
|
|
195
|
+ int month = cale.get(Calendar.MONTH) + 1;
|
|
|
196
|
+ int day = cale.get(Calendar.DAY_OF_MONTH);
|
|
|
197
|
+ return year + "-" + (month < 10 ? "0" + month : month + "") + "-" + (day < 10 ? "0" + day : day + "");
|
|
|
198
|
+ }
|
|
|
199
|
+
|
|
|
200
|
+ /**
|
|
|
201
|
+ * Calendar转为指定格式的日期字符串
|
|
|
202
|
+ * @param cale
|
|
|
203
|
+ * @param format
|
|
|
204
|
+ * @return
|
|
|
205
|
+ */
|
|
|
206
|
+ public static String getDateString(Calendar cale, String format) {
|
|
|
207
|
+ return dateToString(cale.getTime(), format);
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+ /**
|
|
|
211
|
+ *计算两个日期之间相差的时间
|
|
|
212
|
+ * @param sDate
|
|
|
213
|
+ * @param eDate
|
|
|
214
|
+ * @return
|
|
|
215
|
+ * @throws Exception
|
|
|
216
|
+ */
|
|
|
217
|
+ public static long substract(String sDate, String eDate) {
|
|
|
218
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
219
|
+ Date d1 = null;
|
|
|
220
|
+ try {
|
|
|
221
|
+ d1 = sdf.parse(sDate);
|
|
|
222
|
+ } catch (ParseException e) {
|
|
|
223
|
+ e.printStackTrace();
|
|
|
224
|
+ }
|
|
|
225
|
+ Date d2 = null;
|
|
|
226
|
+ try {
|
|
|
227
|
+ d2 = sdf.parse(eDate);
|
|
|
228
|
+ } catch (ParseException e) {
|
|
|
229
|
+ e.printStackTrace();
|
|
|
230
|
+ }
|
|
|
231
|
+ return (d2.getTime() - d1.getTime() + 1000000) / (3600 * 24 * 1000);
|
|
|
232
|
+ }
|
|
|
233
|
+
|
|
|
234
|
+ /**
|
|
|
235
|
+ * 返回当前日期, 格式:yyyy-mm-dd 使用方法: Date.getToday();
|
|
|
236
|
+ * @return 2018-04-21
|
|
|
237
|
+ */
|
|
|
238
|
+ public static String getToday2() {
|
|
|
239
|
+ Calendar rightNow = Calendar.getInstance();
|
|
|
240
|
+ int year = rightNow.get(Calendar.YEAR);
|
|
|
241
|
+ int month = rightNow.get(Calendar.MONTH) + 1;
|
|
|
242
|
+ int day = rightNow.get(Calendar.DAY_OF_MONTH);
|
|
|
243
|
+ return year + "-" + (month < 10 ? "0" + month : month + "") + "-" + (day < 10 ? "0" + day : day + "");
|
|
|
244
|
+ }
|
|
|
245
|
+
|
|
|
246
|
+ /**
|
|
|
247
|
+ * 字符串的日期格式的计算
|
|
|
248
|
+ * @param smdate 较大时结果为负数
|
|
|
249
|
+ * @param bdate 较大时结果为正数
|
|
|
250
|
+ * @return int
|
|
|
251
|
+ * @throws ParseException
|
|
|
252
|
+ */
|
|
|
253
|
+ public static int daysBetween(String smdate, String bdate) throws ParseException {
|
|
|
254
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
255
|
+ Calendar cal = Calendar.getInstance();
|
|
|
256
|
+ cal.setTime(sdf.parse(smdate));
|
|
|
257
|
+ long time1 = cal.getTimeInMillis();
|
|
|
258
|
+ cal.setTime(sdf.parse(bdate));
|
|
|
259
|
+ long time2 = cal.getTimeInMillis();
|
|
|
260
|
+ long between_days = (time2 - time1) / (1000 * 3600 * 24);
|
|
|
261
|
+ return Integer.parseInt(String.valueOf(between_days));
|
|
|
262
|
+ }
|
|
|
263
|
+
|
|
|
264
|
+ /**
|
|
|
265
|
+ * 获取yyyy-MM-dd格式日期的所在星期数
|
|
|
266
|
+ * 例如2018-04-21得到的结果是7
|
|
|
267
|
+ * @param dateStr
|
|
|
268
|
+ * @return
|
|
|
269
|
+ */
|
|
|
270
|
+ public static int getWeekDay(String dateStr) {
|
|
|
271
|
+ int week = -1;
|
|
|
272
|
+ SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
273
|
+ Date date;
|
|
|
274
|
+ try {
|
|
|
275
|
+ date = ft.parse(dateStr);
|
|
|
276
|
+ if (date != null) {
|
|
|
277
|
+ Calendar cal = Calendar.getInstance();
|
|
|
278
|
+ cal.setTime(date);
|
|
|
279
|
+ week = cal.get(Calendar.DAY_OF_WEEK);
|
|
|
280
|
+ }
|
|
|
281
|
+ } catch (ParseException e) {
|
|
|
282
|
+ e.printStackTrace();
|
|
|
283
|
+ }
|
|
|
284
|
+ return week;
|
|
|
285
|
+ }
|
|
|
286
|
+
|
|
|
287
|
+ /**
|
|
|
288
|
+ *
|
|
|
289
|
+ * 将20180421转化为2016-04-21
|
|
|
290
|
+ * @param day
|
|
|
291
|
+ * @return
|
|
|
292
|
+ */
|
|
|
293
|
+ public static String intToDay(long day) {
|
|
|
294
|
+ String dayStr = String.valueOf(day);
|
|
|
295
|
+ return new StringBuilder().append(dayStr.substring(0, 4)).append("-").append(dayStr.substring(4, 6)).append("-").append(dayStr.substring(6, 8)).toString();
|
|
|
296
|
+ }
|
|
|
297
|
+
|
|
|
298
|
+ public static long dayToInt(String day) {
|
|
|
299
|
+ return Long.parseLong(day.replaceAll("-", ""));
|
|
|
300
|
+ }
|
|
|
301
|
+
|
|
|
302
|
+
|
|
|
303
|
+ /**
|
|
|
304
|
+ * 判断某一日期是星期几,星期天为第7天
|
|
|
305
|
+ * @param day
|
|
|
306
|
+ * @return
|
|
|
307
|
+ */
|
|
|
308
|
+ public static String getDayOfWeekCh(String day) {
|
|
|
309
|
+ int dayInt;
|
|
|
310
|
+ Calendar cal = Calendar.getInstance();
|
|
|
311
|
+ cal.set(Integer.parseInt(day.substring(0, 4)), Integer.parseInt(day.substring(5, 7)) - 1, Integer.parseInt(day.substring(8, 10)), 0, 0, 0);
|
|
|
312
|
+ dayInt = cal.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
313
|
+ if (dayInt == 0) {
|
|
|
314
|
+ dayInt = 7;
|
|
|
315
|
+ }
|
|
|
316
|
+ return dayInt + "";
|
|
|
317
|
+ }
|
|
|
318
|
+
|
|
|
319
|
+ /**
|
|
|
320
|
+ * 日期解析
|
|
|
321
|
+ * @param source
|
|
|
322
|
+ * @param format
|
|
|
323
|
+ * @return
|
|
|
324
|
+ * @throws ParseException
|
|
|
325
|
+ */
|
|
|
326
|
+ public static Date parseDate(String source, String format) {
|
|
|
327
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
328
|
+ try {
|
|
|
329
|
+ return sdf.parse(source);
|
|
|
330
|
+ } catch (ParseException e) {
|
|
|
331
|
+ return null;
|
|
|
332
|
+ }
|
|
|
333
|
+ }
|
|
|
334
|
+
|
|
|
335
|
+ /**
|
|
|
336
|
+ * 获取指定年月份的第一天
|
|
|
337
|
+ * @param year
|
|
|
338
|
+ * @param month
|
|
|
339
|
+ * @return
|
|
|
340
|
+ */
|
|
|
341
|
+ public static String monthFirstDay(int year, int month) {
|
|
|
342
|
+ Calendar cal = Calendar.getInstance();
|
|
|
343
|
+ //设置年份
|
|
|
344
|
+ cal.set(Calendar.YEAR, year);
|
|
|
345
|
+ //设置月份
|
|
|
346
|
+ cal.set(Calendar.MONTH, month - 1);
|
|
|
347
|
+ //获取某月最小天数
|
|
|
348
|
+ int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
|
|
|
349
|
+ //设置日历中月份的最小天数
|
|
|
350
|
+ cal.set(Calendar.DAY_OF_MONTH, firstDay);
|
|
|
351
|
+ //格式化日期
|
|
|
352
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
353
|
+ String firstDayOfMonth = sdf.format(cal.getTime());
|
|
|
354
|
+ return firstDayOfMonth;
|
|
|
355
|
+ }
|
|
|
356
|
+
|
|
|
357
|
+ /**
|
|
|
358
|
+ * 获取指定年月份的最后一天
|
|
|
359
|
+ * @param year
|
|
|
360
|
+ * @param month
|
|
|
361
|
+ * @return
|
|
|
362
|
+ */
|
|
|
363
|
+ public static String monthLastDay(int year, int month) {
|
|
|
364
|
+ Calendar cal = Calendar.getInstance();
|
|
|
365
|
+ //设置年份
|
|
|
366
|
+ cal.set(Calendar.YEAR, year);
|
|
|
367
|
+ //设置月份
|
|
|
368
|
+ cal.set(Calendar.MONTH, month - 1);
|
|
|
369
|
+ //获取某月最小天数
|
|
|
370
|
+ int firstDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
371
|
+ //设置日历中月份的最小天数
|
|
|
372
|
+ cal.set(Calendar.DAY_OF_MONTH, firstDay);
|
|
|
373
|
+ //格式化日期
|
|
|
374
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
375
|
+ String lastDayOfMonth = sdf.format(cal.getTime());
|
|
|
376
|
+ return lastDayOfMonth;
|
|
|
377
|
+ }
|
|
|
378
|
+
|
|
|
379
|
+
|
|
|
380
|
+ /**
|
|
|
381
|
+ * Date类型转LocalDate类型
|
|
|
382
|
+ * @param date
|
|
|
383
|
+ * @return
|
|
|
384
|
+ */
|
|
|
385
|
+ public static LocalDate dateToLocalDate(Date date) {
|
|
|
386
|
+ Instant instant = date.toInstant();
|
|
|
387
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
388
|
+ LocalDate localPriceDate = instant.atZone(zoneId).toLocalDate();
|
|
|
389
|
+ return localPriceDate;
|
|
|
390
|
+ }
|
|
|
391
|
+
|
|
|
392
|
+ /**
|
|
|
393
|
+ * LocalDate类型转Date类型
|
|
|
394
|
+ * @param localDate
|
|
|
395
|
+ * @return Date
|
|
|
396
|
+ */
|
|
|
397
|
+ public static Date localDateToDate(LocalDate localDate) {
|
|
|
398
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
399
|
+ ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
|
|
|
400
|
+ Date date = Date.from(zdt.toInstant());
|
|
|
401
|
+ return date;
|
|
|
402
|
+ }
|
|
|
403
|
+
|
37
|
} |
404
|
} |