DateUtil.java
16.2 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package com.tianbo.util.Date;
import javafx.util.converter.LocalDateTimeStringConverter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Calendar;
import java.util.Date;
/**
*
* 日期工具类
* @since 1.0
* @date 2019-04-21
* @author mrz
*
*/
public final class DateUtil {
public static String F19 = "yyyy-MM-dd HH:mm:ss";
public static String F14 = "yyyyMMddHHmmss";
public static String F17 = "yyyyMMddHHmmssSSS";
public static String F10 = "yyyy-MM-dd";
public static String F8 = "yyyyMMdd";
public static String F6 = "yyyyMM";
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"};
private static Date currentDate = new Date();
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat sdf_yyyyMMdd = new SimpleDateFormat(F8);
private static SimpleDateFormat timesdf = new SimpleDateFormat(F14);
private static SimpleDateFormat timesdfsss = new SimpleDateFormat(F17);
public static String getToday(){
return sdf.format(currentDate);
}
public static String getTodayBy_yyyyMMdd(){
return sdf_yyyyMMdd.format(new Date());
}
public static String get_yyyyMMdd(String date) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parse = simpleDateFormat.parse(date);
return sdf_yyyyMMdd.format(parse);
}
public static Date formatByyyyyMMdd(String dateStr){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate beginDateTime = LocalDate.parse(dateStr, formatter);
Date date = localDateToDate(beginDateTime);;
return date;
}
/**
* 用localDatetime转毫秒字符串为时间,JAVA8有BUG,改为用simpleDateFormat转换,这个方法暂时不可用
* @param dateStr
* @return
*/
public static Date formatByF17(String dateStr){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(F17);
LocalDateTime beginDateTime = LocalDateTime.parse(dateStr, formatter);
Date date = convertLDTToDate(beginDateTime);;
return date;
}
/**
* 取当前时间
* @return 20190916071330
*/
public static String getDDTM(){
return timesdf.format(currentDate);
}
/**
* 取当前时间
* @return 20190916071330431
*/
public static String getCurrentTime17(){
return timesdfsss.format(new Date());
}
/**
* 日期格式转换返回
* @param dateStr
* @return 20190916071330
* @throws DateTimeParseException
*/
public static Date formatByyyyyMMddHHmmss(String dateStr) throws DateTimeParseException{
//毫秒级的去掉
if(dateStr.length()>14){
dateStr= dateStr.substring(0,14);
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
return convertLDTToDate(dateTime);
}
//LocalDateTime转换为Date
public static Date convertLDTToDate(LocalDateTime time) {
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* 将字符串转换为long类型的值(不包含-符号)
* @param dateString 2016-10-12
* @return 20161012
*/
public static long stringToDateLong(String dateString) {
String[] dates = dateString.split("-");
return Long.valueOf(dates[0] + dates[1] + dates[2]);
}
/**
* 将日期转化为默认的格式显示
* @param date Date实例
* @return 2017-06-06
*/
public static String dateToString(Date date) {
return dateToString(date, F10);
}
/**
* @param date Date实例
* @param format yyyy-MM-dd
* @return 2017-06-06
*/
public static String dateToString(Date date, String format) {
if (date == null) {
return null;
}
SimpleDateFormat sf = new SimpleDateFormat(format);
return sf.format(date);
}
/**
* 时间戳转默认字符串日期
* @param time 1496739850253
* @return 2017-06-06
*/
public static String longToString(long time) {
return longToString(time, F10);
}
/**
* 时间戳转字符串日期(格式可以自己选择)
* @param time 1496739850253
* @param format yyyy-MM-dd
* @return 2017-06-06
*/
public static String longToString(long time, String format) {
SimpleDateFormat sf = new SimpleDateFormat(format);
return sf.format(new Date(time));
}
/**
* 在指定日期上加上一定天数获得新的日期
* @param day 2016-06-06
* @param addDay 10
* @return 2016-06-16
*/
public static String getNextDay(String day, int addDay) {
Calendar calendar = getCalendar(day);
calendar.add(Calendar.DAY_OF_MONTH, addDay);
return getDateString(calendar);
}
/**
* 获取当前时间(包含小时、分、秒)
* @return 2016-06-06 10:20:10
*/
public static String getCurrTime() {
return dateToString(new Date(), F19);
}
/**
* 获取当前日期(不包含小时、分、秒)
* @return 2016-06-06
*/
public static String getCurrDate() {
return dateToString(new Date(), F10);
}
/**
* 获取日期相距天数
* @param startDate 2016-06-06
* @param endDate 2016-06-10
* @return int 4
*/
public static int getCompareDate(String startDate, String endDate) {
try {
SimpleDateFormat formatter = new SimpleDateFormat(F10);
Date date1 = formatter.parse(startDate);
Date date2 = formatter.parse(endDate);
long l = date2.getTime() - date1.getTime() + 1000;
long d = l / (24 * 60 * 60 * 1000);
return (int) d;
} catch (ParseException e) {
}
return 0;
}
private static Calendar getCalendar(String day) {
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(day.substring(0, 4)), Integer.parseInt(day.substring(5, 7)) - 1, Integer.parseInt(day.substring(8, 10)), 0, 0, 0);
return cal;
}
/**
* 当前时间几小时相差多少时间
* @param hour
* @return
* @since 2017年8月18日
*/
public static Date getAddHourTime(int hour) {
Calendar dalendar = Calendar.getInstance();
dalendar.add(Calendar.HOUR_OF_DAY, hour);
return dalendar.getTime();
}
/**
* 将Calendar转换为日期字符串(字符串的格式:2018-04-21)
* @param cale
* @return
*/
public static String getDateString(Calendar cale) {
int year = cale.get(Calendar.YEAR);
int month = cale.get(Calendar.MONTH) + 1;
int day = cale.get(Calendar.DAY_OF_MONTH);
return year + "-" + (month < 10 ? "0" + month : month + "") + "-" + (day < 10 ? "0" + day : day + "");
}
/**
* Calendar转为指定格式的日期字符串
* @param cale
* @param format
* @return
*/
public static String getDateString(Calendar cale, String format) {
return dateToString(cale.getTime(), format);
}
/**
*计算两个日期之间相差的时间
* @param sDate
* @param eDate
* @return
* @throws Exception
*/
public static long substract(String sDate, String eDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = null;
try {
d1 = sdf.parse(sDate);
} catch (ParseException e) {
e.printStackTrace();
}
Date d2 = null;
try {
d2 = sdf.parse(eDate);
} catch (ParseException e) {
e.printStackTrace();
}
return (d2.getTime() - d1.getTime() + 1000000) / (3600 * 24 * 1000);
}
/**
* 返回当前日期, 格式:yyyy-mm-dd 使用方法: Date.getToday();
* @return 2018-04-21
*/
public static String getToday2() {
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(Calendar.YEAR);
int month = rightNow.get(Calendar.MONTH) + 1;
int day = rightNow.get(Calendar.DAY_OF_MONTH);
return year + "-" + (month < 10 ? "0" + month : month + "") + "-" + (day < 10 ? "0" + day : day + "");
}
/**
* 字符串的日期格式的计算
* @param smdate 较大时结果为负数
* @param bdate 较大时结果为正数
* @return int
* @throws ParseException
*/
public static int daysBetween(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 获取yyyy-MM-dd格式日期的所在星期数
* 例如2018-04-21得到的结果是7
* @param dateStr
* @return
*/
public static int getWeekDay(String dateStr) {
int week = -1;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = ft.parse(dateStr);
if (date != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
week = cal.get(Calendar.DAY_OF_WEEK);
}
} catch (ParseException e) {
e.printStackTrace();
}
return week;
}
/**
*
* 将20180421转化为2016-04-21
* @param day
* @return
*/
public static String intToDay(long day) {
String dayStr = String.valueOf(day);
return new StringBuilder().append(dayStr.substring(0, 4)).append("-").append(dayStr.substring(4, 6)).append("-").append(dayStr.substring(6, 8)).toString();
}
public static long dayToInt(String day) {
return Long.parseLong(day.replaceAll("-", ""));
}
/**
* 判断某一日期是星期几,星期天为第7天
* @param day
* @return
*/
public static String getDayOfWeekCh(String day) {
int dayInt;
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(day.substring(0, 4)), Integer.parseInt(day.substring(5, 7)) - 1, Integer.parseInt(day.substring(8, 10)), 0, 0, 0);
dayInt = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (dayInt == 0) {
dayInt = 7;
}
return dayInt + "";
}
/**
* 日期解析
* @param source
* @param format
* @return
* @throws ParseException
*/
public static Date parseDate(String source, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(source);
} catch (ParseException e) {
return null;
}
}
/**
* 获取指定年月份的第一天
* @param year
* @param month
* @return
*/
public static String monthFirstDay(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month - 1);
//获取某月最小天数
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH, firstDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String firstDayOfMonth = sdf.format(cal.getTime());
return firstDayOfMonth;
}
/**
* 获取指定年月份的最后一天
* @param year
* @param month
* @return
*/
public static String monthLastDay(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month - 1);
//获取某月最小天数
int firstDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH, firstDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* Date类型转LocalDate类型
* @param date
* @return
*/
public static LocalDate dateToLocalDate(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDate localPriceDate = instant.atZone(zoneId).toLocalDate();
return localPriceDate;
}
/**
* LocalDate类型转Date类型
* @param localDate
* @return Date
*/
public static Date localDateToDate(LocalDate localDate) {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
Date date = Date.from(zdt.toInstant());
return date;
}
/**java.time.LocalDateTime --> java.util.Date
*
*/
public void LocalDateTimeToUdate() {
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
/**
* 根据出生日期计算年龄
* @param birthDay
* @return
* @throws ParseException
*/
public static int getAgeByBirth(Date birthDay) throws ParseException {
int age = 0;
Calendar cal = Calendar.getInstance();
//出生日期晚于当前时间,无法计算
if (cal.before(birthDay)) {
throw new IllegalArgumentException(
"The birthDay is before Now.It's unbelievable!");
}
//当前年份
int yearNow = cal.get(Calendar.YEAR);
//当前月份
int monthNow = cal.get(Calendar.MONTH);
//当前日期
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
//计算整岁数
age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
//当前日期在生日之前,年龄减一
if (dayOfMonthNow < dayOfMonthBirth){ age--;}
} else {
age--;//当前月份在生日之前,年龄减一
}
}
return age;
}
/**
* 给时间加上几个小时
* @param day 当前时间 格式:yyyy-MM-dd HH:mm:ss
* @param hour 需要加的时间
* @return
*/
public static String addDateMinut(Date day, int hour){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = null;
try {
date = day;
} catch (Exception ex) {
ex.printStackTrace();
}
if (date == null)
return "";
// System.out.println("front:" + format.format(date)); //显示输入的日期
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, hour);// 24小时制
date = cal.getTime();
// System.out.println("after:" + format.format(date)); //显示更新后的日期
cal = null;
return format.format(date);
}
/**
* 传入时间+15分钟
* @param date
* @return
* @throws ParseException
*/
public static Date dateTimeAddfifteen(Date date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar nowTime = Calendar.getInstance();
nowTime.setTime(date);
nowTime.add(Calendar.MINUTE, 15);
Date time = nowTime.getTime();
return time;
}
}