1. Preface
In Java development, the processing of date and time has gone through several stages with the iterative development of JDK, from the initialand
To Java 8 (
Classes under the package) introduce a brand new date and time API. Today in Java, JDK provides a variety of date and time related classes, mainly including the following:
-
: This is the earliest provided by Java to represent a specific time instant, precise to milliseconds. Can be passed
()
to get the millisecond representation of the current time. -
: This is a date class designed for database applications, which only contains year, month, and day information, and does not contain time and time zone information.
-
: Similar, but it contains only hour, minute and second information.
-
: This is a more accurate timestamp class than it can be accurate to the nanosecond level.
-
and
: Calendar is an abstract class that provides calculation methods for dates and times.
GregorianCalendar
yesCalendar
A concrete implementation of the Gregorian calendar (Gregorian calendar). -
Classes in the package: in Java 8 and above, it is recommended to use
Classes in packages handle dates and times because they are more rationally designed and more powerful. The main categories include
LocalDate
、LocalTime
、LocalDateTime
、ZonedDateTime
、Instant
、Duration
、Period
wait.
2. Date format conversion
LocalDateTime and Date convert each other
Date to LocalDateTime, LocalDate, LocalTime
To convert to LocalDate, LocalTime, or LocalDateTime, you can use the class as an intermediate bridge, because the Instant class can represent an instantaneous point on the timeline, with similar uses (although Instant is represented in UTC, while Date is relative to the default time zone).
When Java8 was released, the conversion between the old and new APIs was also considered, soAdded
toInstance()
Method, you can get aInstance
Example. Instance represents an instant point on the timeline, and there is no concept of time zone, so we need to passatZone()
Specify the time zone and getZonedDateTime
Instance, then convert to。
as follows:
Date dateNow = new Date(); LocalDateTime localDateTime = () .atZone(()) .toLocalDateTime() LocalDate localDate = () .atZone(()) .toLocalDate(); LocalTime localTime = () .atZone(()) .toLocalTime();
Similarly, we can also create instances with the help ofEpochMilli() method of Instance. as follows:
Date dateNow = new Date(); LocalDateTime localDateTime = (()) .atZone(()) .toLocalDateTime(); LocalDate localDate = (()) .atZone(()) .toLocalDate(); LocalTime localTime = (()) .atZone(()) .toLocalTime(); }
In the above example, the object is first converted to, and then converted to a ZonedDateTime object with a specific time zone (here is the system default time zone) via the atZone(()) method. Since LocalDate, LocalTime, and LocalDateTime are time zone-independent (for LocalDateTime, it is actually "local" and does not contain time zone information, but gets it from Instant by specifying the time zone), they are finally extracted from ZonedDateTime by calling the .toLocalDate(), .toLocalTime() or .toLocalDateTime() methods.
Note that during the conversion process, you need to specify a time zone because it does not contain time zone information itself, it is just the number of milliseconds since epoch. Therefore, when converting to LocalDate, LocalTime, or LocalDateTime, it is necessary to clarify which time zone you want the date or time of your desired time. If you want UTC time, you can use ("UTC") instead of().
LocalDateTime, LocalDate, LocalTime to Date
LocalDateTime localDateTime = (); Date dateFromLocalDateTime = ((()).toInstant()); // Since `LocalDate` does not contain time information, the time must be set to transfer Date. atStartOfDay, automatically assign midnight time, return LocalDateTime, set the time zone and return ZonedDateTime, and then get InstantLocalDate localDate = (); Date dateFromLocalDate = (().atZone(()).toInstant()); // Since `LocalDate` does not contain date information, a date must be set to transfer Date.LocalTime localTime = (); Date dateFromLocalTime = ((()).atZone(()).toInstant());
LocalDateTime and String convert each other
LocalDateTime to String
The LocalDate class has a format() method that converts dates into strings. The format() method requires a DateTimeFormatter object as a parameter.
DateTimeFormatter dateTimeFormatter = ("yyyy-MM-dd HH:mm:ss"); String dateTimeStr = ().format(dateTimeFormatter);
String to LocalDateTime
We can use the parse() method to parse the date object from a string
String dateTimeStr = ().format(("yyyy-MM-dd HH:mm:ss")); LocalDateTime localDateTime = (dateTimeStr);
LocalDateTime and Instant convert each other
LocalDateTime
Indicates a date and time without a time zone, such as 2019-10-25T12:15:30, andInstant
It is an instant point on the timeline.
LocalDateTime to Instant
LocalDateTime localDateTime = (); Instant instant; //The first method: Using ()instant = (); //The second method: Using () and ()instant =(()).toInstant(); //The third method: Using () and ()long timeInSeconds = (); instant = (timeInSeconds);
Instant to LocalDateTime
//Using LocalDateTime localDateTime = ((), ()); long timeInSeconds = 1567109422L; localDateTime = ((timeInSeconds), ()); localDateTime = ((timeInSeconds, 0), ()); long timeInMillis = 1567109422123L; localDateTime = ((timeInMillis), ()); //Using Timestamp localDateTime = (()).toLocalDateTime();
Date and String convert each other
Date to String
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); String dateStr = (now);
String to Date
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); String dateStr = (now); Date date = (dateStr);
Summarize
This is the end of this article about various date and time conversion in Java. For more relevant contents of Java Date and LocalDateTime conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!