overview
Introduction
The DateTime package is one of the core packages of Hutool, providing encapsulation for the Date and Calendar objects in the JDK. The encapsulated objects are as follows:
DateTime Tools
DateUtil
provides a series of static methods for date and time operations.DateTime
provides encapsulation for date and time objects similar to Joda-Time, inheriting from the Date class and providing more extensive object methods.FastDateFormat
provides thread-safe formatting and date string parsing support for Date objects. This object is not needed to be perceived in actual use, and the relevant operations have been encapsulated in the relevant methods ofDateUtil
andDateTime
.DateBetween
is a class for calculating the time interval between two times, in addition to using a new object for construction, the relevant operations have also been encapsulated in the relevant methods ofDateUtil
andDateTime
.TimeInterval
is a simple timer class, often used to calculate the execution time of a certain section of code, providing various unit calculations including milliseconds, seconds, minutes, hours, days, and weeks, with static construction encapsulated inDateUtil
.DatePattern
provides common date formatting patterns, including both String and FastDateFormat types.
Date Enums
Considering that the fields representing time in the Calendar
class are represented by int
, which is inconvenient in use, encapsulation has been made for these int
fields with corresponding Enum enums. These Enum classes are used as parameters in the relevant methods of DateUtil
and DateTime
, allowing for greater parameter scope reduction.
These defined enum values can obtain their corresponding int values with the getValue()
method and convert int values from Calendar
to enum objects with the of(int)
method.
The following Enums corresponding to Calendar
are included:
Month
represents months, with one-to-one correspondence with int values in Calendar.Week
represents weeks, also with one-to-one correspondence with int values in Calendar.
Months Enumerations
The month enumeration can be used to obtain the last day of a specific month as follows:
// 31
int lastDay = Month.of(Calendar.JANUARY).getLastDay(false);
In addition, Hutool has defined a season enum. Season.SPRING
represents the first quarter of the year (January to March). The concept of season is not equivalent to calendar seasons since months do not necessarily correspond to seasons, but seasons are often used for statistical purposes.
Time Enumerations
The DateUnit
enum mainly represents the milliseconds corresponding to a specific time unit, often used for calculating time intervals. For example: DateUnit.MINUTE
represents minutes or 60 seconds, and its milliseconds can be obtained by calling its getMillis()
method.