This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
❤️ About the author: Hello everyone, I am Xiao Xu Zhu. Java field quality creator 🏆, CSDN blog expert certification 🏆, Huawei Cloud enjoy expert certification 🏆
❤️ technology live, the appreciation
❤️ like 👍 collect ⭐ look again, form a habit
Before reading this article, it is recommended to have some knowledge of the date and time of Java source code. If not, you can read this article first:
Swastika blog teaches you to understand Java source code date and time related usage
Related articles:
Hutool actual combat (take you to master the various tools) directory
2HUtool Combat :DateUtil- Common time type conversion
Source code analysis purposes
Know what it is and why
Project reference
The basis of this blog: Hutool -5.6.5 version of the source code
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>
Copy the code
Dateutil. date(java.util.date)
Methods described
{@link Date} type time to {@link DateTime}<br> If Date itself is a DateTime object, the strong object is returned, otherwise a new DateTime object is created
Source code Analysis 1
/ * * * {@linkDate} Type time changes to {@linkDateTime}<br> * If date itself is a DateTime object, return the strong object, otherwise create a new DateTime object * *@paramDate Long Type date (Unix timestamp) *@returnTime object@since3.0.7 * /
public static DateTime date(Date date) {
if (date instanceof DateTime) {
return (DateTime) date;
}
return dateNew(date);
}
Copy the code
Determine if the date passed is of type DateTime, and if so, convert it directly to a DateTime object
If not, execute dateutil.datenew ((date)
The method name: DateUtil. DateUtil. DateNew (Java. Util. Date)
Methods described
Create a new {@link DateTime} object based on the existing {@link Date} object
Source code Analysis 1
/** *@linkDate} produces a new {@linkDateTime} object * *@paramDate Date object *@return {@linkA DateTime object} *@since4.3.1 * /
public static DateTime dateNew(Date date) {
return newDateTime(date); } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --/ / DateTime class
/** * The construction of the given date **@paramThe date date * /
public DateTime(Date date) {
this(
date,//
(date instanceof DateTime) ? ((DateTime) date).timeZone : TimeZone.getDefault()
);
}
Copy the code
In the code, there is a multi-entry operation, (date instanceof DateTime)? ((DateTime) date).timeZone: timezone.getDefault ().
Check whether the date type is DateTime. If yes, use the time zone of the DateTime object. If no, use the default time zone of the system.
And then call this method DateTime(Date Date, TimeZone TimeZone)
/ / DateTime class
/** * The construction of the given date **@paramThe date date *@param* timeZone time zone@since4.1.2 * /
public DateTime(Date date, TimeZone timeZone) {
this(ObjectUtil.defaultIfNull(date, new Date()).getTime(), timeZone);
}
Copy the code
ObjectUtil.defaultIfNull(date, new Date()).getTime()
To split into 2 calls
1, objectutil.defaultifNull (date, new date ())
2, Date. GetTime ()
Objectutil. defaultIfNull is a utility class wrapped by Hutool that determines whether a data object is null and returns the default if so
The source code is as follows
/** * if the given object is {@codeNull} Returns the default value * * <pre> * objectutil. defaultIfNull(NULL, NULL) = null * objectutil. defaultIfNull(null, "") = "" * ObjectUtil.defaultIfNull(null, "zz") = "zz" * ObjectUtil.defaultIfNull("abc", *) = "abc" * ObjectUtil.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE * </pre> * *@param<T> Object type *@paramObject The object to be checked. This may be {@code null}
* @paramDefaultValue The checked object is {@codeThe default value returned by null} can be {@code null}
* @returnThe checked object is {@codeNull} returns the default value, otherwise returns the original value *@since3.0.7 * /
public static <T> T defaultIfNull(final T object, final T defaultValue) {
return (null! = object) ? object : defaultValue; }Copy the code
This method is called at the end
/** * Construct ** for a given date number of milliseconds@paramTimeMillis Date number of milliseconds *@param* timeZone time zone@since4.1.2 * /
public DateTime(long timeMillis, TimeZone timeZone) {
super(timeMillis);
this.timeZone = ObjectUtil.defaultIfNull(timeZone, TimeZone.getDefault());
}
Copy the code
**DateTime(long timeMillis, TimeZone TimeZone); **DateTime(long Date, TimeZone TimeZone);
Dateutil.dateutil.date (long)
Methods described
{@link DateTime}<br> Only supports the millisecond level timestamp. If you need the second level timestamp, please ×1000
Source code Analysis 1
/** * Long;@linkDateTime}<br> * Only millisecond level timestamp is supported. If you need second level timestamp, please ×1000 * *@paramDate Long Type date (Unix timestamp) *@returnTime object */
public static DateTime date(long date) {
return newDateTime(date); } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --/** * Construct ** for a given date number of milliseconds@paramTimeMillis Date number of milliseconds *@since4.1.2 * /
public DateTime(long timeMillis) {
this(timeMillis, TimeZone.getDefault());
}
Copy the code
**DateTime(long timeMillis, TimeZone TimeZone); **DateTime(long Date, TimeZone TimeZone);
Dateutil.dateutil. date(java.util.calendar)
Methods described
Conversion of {@link Calendar} type time to {@link DateTime}<br> Always generates a new {@link DateTime} object based on the existing {@link Calendar}
Source code Analysis 1
/ * * * {@linkCalendar} type time converted to {@linkDateTime}<br> * always based on existing {@linkCalendar} produces a new {@linkDateTime} object * *@param calendar {@link Calendar}
* @returnTime object */
public static DateTime date(Calendar calendar) {
return newDateTime(calendar); } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --/ / DateTime class
/** * The construction of the given date **@param calendar {@link Calendar}
*/
public DateTime(Calendar calendar) {
this(calendar.getTime(), calendar.getTimeZone());
this.setFirstDayOfWeek(Week.of(calendar.getFirstDayOfWeek()));
}
Copy the code
**this(calendar.getTime(), calendar.getTimeZone()); DateTime(Date Date, TimeZone TimeZone)
this.setFirstDayOfWeek(Week.of(calendar.getFirstDayOfWeek()));
Copy the code
There are actually three steps called:
1, calendar. GetFirstDayOfWeek () to obtain calendarWeekIntValue (the first day of the week)
2, Week) of (calendar. GetFirstDayOfWeek () – > Week of (int calendarWeekIntValue) corresponding enumeration values for Week
3, enclosing setFirstDayOfWeek (Week) of (calendar. GetFirstDayOfWeek ())) – > setFirstDayOfWeek (Week firstDayOfWeek) Sets the value of the first day of the week parameter to the DateTime object.
/** * Set the first day of the week <br> * the JDK Calendar defaults to the first day of the week on Sunday. Hutool sets this default value to Monday.@link# weekOfMonth ()} and {@link#weekOfYear()} two methods * *@paramFirstDayOfWeek The first day of the week *@return this
* @see #weekOfMonth()
* @see #weekOfYear()
*/
public DateTime setFirstDayOfWeek(Week firstDayOfWeek) {
this.firstDayOfWeek = firstDayOfWeek;
return this;
}
Copy the code
Method name: DateUtil DateUtil. Date (Java. Time. Temporal. TemporalAccessor)
Methods described
{@link TemporalAccessor} Type time to {@link DateTime}< BR > Always generate a new {@link DateTime} object based on the existing {@Link TemporalAccessor}
Source code Analysis 1
/ * * * {@linkTemporalAccessor} type time to {@linkDateTime}<br> * always based on existing {@linkTemporalAccessor} generates new {@linkDateTime} object * *@param temporalAccessor {@linkTemporalAccessor}, commonly subclass: {@link LocalDateTime}、 LocalDate
* @returnTime object@since5.0.0 * /
public static DateTime date(TemporalAccessor temporalAccessor) {
return newDateTime(temporalAccessor); } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -/ / a DateTime object
/** * Construct of TemporalAccessor for a given date **@param temporalAccessor {@link* TemporalAccessor} object@since5.0.0 * /
public DateTime(TemporalAccessor temporalAccessor) {
this(DateUtil.toInstant(temporalAccessor));
}
Copy the code
**this(DateUtil.toInstant(temporalAccessor)); ** Two steps are called here
1. Dateutil. toInstant(temporalAccessor) The temporalAccessor object becomes an Instant object
2, this (DateUtil toInstant (temporalAccessor)); —>DateTime(Instant instant)
/** * The construction of Instant on the given date **@param instant {@linkInstant *} object@since5.0.0 * /
public DateTime(Instant instant) {
this(instant.toEpochMilli()); } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --/** * Construct ** for a given date number of milliseconds@paramTimeMillis Date number of milliseconds *@since4.1.2 * /
public DateTime(long timeMillis) {
this(timeMillis, TimeZone.getDefault());
}
Copy the code
It’s all about it. Look, it’s coming back