1. Dependency introduction:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>
2. Method analysis:
parse can recognize some common formats of time:
/*yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
HH:mm:ss
yyyy-MM-dd HH:mm
yyyy-MM-dd HH:mm:ss SSS*/
1> Mutual conversion between string and date (Date):
DateUtil.parse(str);
DateUtil.format(date,DatePattern.NORM_DATETIME_PATTERN);
DateUtil.formatDate(date); Result format: yyyy-MM-dd
DateUtil.formatDateTime(date); Result format: yyyy-MM-dd HH:mm:ss SSS
DateUtil.formatTime(date); Result format: HH:mm:ss
2>Conversion between string and date (LocalDateTime):
DateUtil.parseLocalDateTime(str,DatePattern.NORM_DATETIME_PATTERN);
DateUtil.formatLocalDateTime(localDateTime);
3> Get the current time (Date)
Date date = DateUtil.date();
Date date = DateUtil.date(Calendar.getInstance());
Date date = DateUtil.date(System.currentTimeMillis());
4>Get the current time (String)
#Current time string,yyyy-MM-dd HH:mm:ss
String now = DateUtil.now();
#Current date string, yyyy-MM-dd
String today = DateUtil.today();
5>Get a certain part of the Date object
DateUtil.year(date); Get date year
DateUtil.month(date); Get the date and month
DateUtil.monthEnum(date); Gets the date and month enumeration
6> Get the start time and end time of the day (same as getting the start time and end time of the year, quarter, month, week, day, hour, minute, second, millisecond)
DateUtil.beginOfDay(date); yyyy-MM-dd 00:00:00
DateUtil.endOfDay(date); yyyy-MM-dd 23:59:59
7>Get the start time and end time of a month
DateUtil.beginOfMonth(date);
DateUtil.endOfMonth(date);
8>Date time offset (negative number means forward offset)
DateUtil.offset(date,DateField.DAY_OF_MONTH,2);
Result: The date is pushed back two days. 2019-09-17 17:35:35 =>2019-09-19 17:35:35
DateUtil.offsetDay(date,2);
Result: The date is pushed back two days. 2019-09-17 17:35:35 =>2019-09-19 17:35:35
DateUtil.offsetHour(date,2);
Result: The date is pushed back two hours. 2019-09-17 17:35:35 =>2019-09-19 19:35:35
DateUtil.yesterday();
Result: yesterday
DateUtil.tomorrow();
Result: tomorrow
DateUtil.lastWeek();
Result: last week
DateUtil.nextWeek();
Result: next week
DateUtil.lastMonth();
Result: last month
DateUtil.nextMonth();
Result: next month
9>Date time difference
a> Calculate the number of days difference between two dates, exchange the positions, and the output result remains unchanged.
long betweenDay = DateUtil,between(date1,date2,DateUnit.DAY);
long betweenDay = DateUtil,between(date1,date2,DateUnit.DAY,boolean);
long betweenDay =DateUtil.betweenDay(date1,date2,boolean);
2016-02-01 23:59:59 2016-02-02 00:00:00
When boolean is false, the difference between calculation results is 0 days.
When boolean is true, the difference between calculation results is 1 day.
b>Calculate the difference in milliseconds between two dates
long betweenMs = DateUtil.betweenMs(date1,date2);
c> Calculate the number of weeks in the specified time interval
long betweenWeek = DateUtil.betweenWeek(date1,date2,boolean);
d> Calculate the number of months between two dates. In the case of non-reset, if the days of the start date are greater than the days of the end date, the number of months will be counted as 1 less (less than 1 month)
DateUtil.betweenMonth(date1,date2,boolean);
e> Calculate the number of years between two dates. In the case of non-reset, if the month of the start date is greater than the month of the end date, the number of years should be reduced by 1 (less than 1 year)
DateUtil.betweenYear(date1,date2,boolean);
f>Format date interval output
level level, divided into 5 levels according to days, hours, minutes, seconds and milliseconds
DateUtil.formatBetween(date1,date2,BetweenFormatter.Level);
g> Formatted date interval output, accurate to milliseconds
DateUtil.formatBetween(date1,date2);
h> formatted date interval output
DateUtil.formatBetween(long,date1,BetweenFormatter.Level);
i> Whether the current date is within the specified date range (start date and end date can be interchanged)
DateUtil.isIn(date1,date2,date);
j> Whether they are the same time, the principle is to compare whether the timestamps of the two dates are the same
DateUtil.isSameTime(date1,date2);
k>Comparing whether two dates are the same day
DateUtil.isSameDay(date1,date2);
l> Compare whether two dates are in the same month
DateUtil.isSameMonth(date1,date2);
10>Other
Calculate age from date: int ageOfNow = DateUtil.ageOfNow(date);
Determine whether it is a leap year: DateUtil.isLeapYear(year);
Get the total number of days in the specified year: DateUtil.lengthOfYear(int);
Get the total number of days in the specified month: DateUtil.lengthOfMonth(int,boolean);
Convert time format string to seconds: DateUtil.timeToSecond(String);
Convert seconds to time format: DateUtil.secondToTime(int);