Date related classes in Java 8
Origin of Java 8 date class
JDK 1.0 includes a java.util.Date class, but most of its methods have been deprecated after JDK 1.1 introduced the calendar class. Calendar is not much better than date. The problems they face are:
-
The year in the Date class starts from 1900, and the month starts from 0.
-
Formatting is only useful for the Date class, but not for the Calendar class.
-
Non thread safety, etc.
Overview of Java 8 date classes
-
Java 8 further strengthens the processing of date and time by publishing a new date time API.
-
java.time package: the package is the base package of the date / time API.
-
java.time.chrono package: this package provides access to different calendar systems.
-
java.time.format package: this package can format and parse date time objects.
-
Java.time.temporary package: this package contains the underlying framework and extension features.
-
java.time.zone package: this package supports classes with different time zones and related rules.
Overview of the LocalDate class
(1) Basic concepts
- java.time.LocalDate class is mainly used to describe date information in year month day format. This class does not represent time and time zone information.
(2) Common methods
Method declaration | Function introduction |
---|---|
static LocalDate now() | Gets the current date from the system clock in the default time zone |
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class LocalDateTimeTest { public static void main(String[] args) { // 1. Obtain the current date information and print it LocalDate now = LocalDate.now(); System.out.println("The current date obtained is:" + now); } }
The current date obtained is 2021-09-19
Overview of the LocalTime class
(1) Basic concepts
- The java.time.LocalTime class is mainly used to describe time information, which can describe hours, minutes, seconds and nanoseconds.
(2) Common methods
Method declaration | Function introduction |
---|---|
static LocalTime now() | Gets the current time from the system time in the default time zone |
static LocalTime now(ZoneId zone) | Gets the current time in the specified time zone |
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; public class LocalDateTimeTest { public static void main(String[] args) { // 2. Obtain the current time information and print it LocalTime now1 = LocalTime.now(); System.out.println("The current time obtained is:" + now1); } }
The current time obtained is: 01:23:31.054464300
Overview of the LocalDateTime class
(1) Basic concepts
- The java.time.LocalDateTime class is mainly used to describe the date and time without time zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
(2) Common methods
Method declaration | Function introduction |
---|---|
static LocalDateTime now() | Gets the current date and time from the system time in the default time zone |
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) | Set the date and time according to the year, day, hour, minute and second information specified in the parameter |
int getYear() | Gets the value of the year field |
int getMonthValue() | Get date field |
int getHour() | Get hours |
int getMinute() | Get minutes |
int getSecond() | Get seconds |
LocalDateTime withYear(int year) | Set the year specified for the parameter |
LocalDateTime withMonth(int month) | Set the month specified for the parameter |
LocalDateTime withDayOfMonth(int dayOfMonth) | Sets the day specified for the parameter |
LocalDateTime withHour(int hour) | When set to the value specified by the parameter |
LocalDateTime withMinute(int minute) | Set the minute specified for the parameter |
LocalDateTime withSecond(int second) | Set the second specified for the parameter |
LocalDateTime plusYears(long years) | Add the year specified by the parameter |
LocalDateTime plusMonths(long months) | Add the month specified by the parameter |
LocalDateTime plusDays(long days) | Add the day specified by the parameter |
LocalDateTime plusHours(long hours) | When the value specified by the parameter is added |
LocalDateTime plusMinutes(long minutes) | Add the score specified by the parameter |
LocalDateTime plusSeconds(long seconds) | Plus the second specified by the parameter |
LocalDateTime minusYears(long years) | Subtract the year specified by the parameter |
LocalDateTime minusMonths(long months) | Subtract the month specified by the parameter |
LocalDateTime minusDays(long days) | Subtract the day specified by the parameter |
LocalDateTime minusHours(long hours) | When subtracting the value specified by the parameter |
LocalDateTime minusMinutes(long minutes) | Subtract the score specified by the parameter |
LocalDateTime minusSeconds(long seconds) | Subtracts the second specified by the parameter |
import java.time.LocalDateTime; public class LocalDateTimeTest { public static void main(String[] args) { // 3. Obtain and print the current date and time information, which can be used at most LocalDateTime now2 = LocalDateTime.now(); System.out.println("The current date and time obtained is:" + now2); System.out.println("-------------------------------------------------------"); // 4. Use the year, day, hour, minute and second information specified by the parameter to obtain the object and print it // Use ctrl+F12 to find the specified method LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8); System.out.println("The specified date and time is:" + of); // Automatically call toString method System.out.println("The year obtained is:" + of.getYear()); // 2008 System.out.println("The months obtained are:" + of.getMonthValue()); // 8 System.out.println("The date obtained is:" + of.getDayOfMonth()); // 8 System.out.println("The obtained is:" + of.getHour()); // 20 System.out.println("The scores obtained are:" + of.getMinute()); // 8 System.out.println("The seconds obtained are:" + of.getSecond()); // 8 System.out.println("-------------------------------------------------------"); // 5. Realize feature setting and printing // Similar to the String type, the data content of the calling object itself will not change, and the return value is equivalent to creating a new object, which proves immutability LocalDateTime localDateTime = of.withYear(2012); System.out.println("localDateTime = " + localDateTime); // 2012-08-08T20:08:08 System.out.println("of = " + of); // 2008-08-08T20:08:08 LocalDateTime localDateTime1 = localDateTime.withMonth(12); System.out.println("localDateTime1 = " + localDateTime1); // 2012 12 8 20 8 8 System.out.println("-------------------------------------------------------"); // 6. Add and print features LocalDateTime localDateTime2 = localDateTime1.plusDays(2); System.out.println("localDateTime2 = " + localDateTime2); // 2012 12 10 20 8 8 System.out.println("localDateTime1 = " + localDateTime1); // 2012 12 8 20 8 8 LocalDateTime localDateTime3 = localDateTime2.plusHours(3); System.out.println("localDateTime3 = " + localDateTime3); // 2012 12 10 23 8 8 System.out.println("-------------------------------------------------------"); // 7. Reduce features and print LocalDateTime localDateTime4 = localDateTime3.minusMinutes(1); System.out.println("localDateTime4 = " + localDateTime4); // 2012 12 10 23 7 8 System.out.println("localDateTime3 = " + localDateTime3); // 2012 12 10 23 8 8 LocalDateTime localDateTime5 = localDateTime4.minusSeconds(3); System.out.println("localDateTime5 = " + localDateTime5); // 2012 12 10 23 7 5 } }
The current date and time obtained is 2021-09-19T01:24:30.515832200 ------------------------------------------------------- The specified date and time is: 2008-08-08T20:08:08 The year obtained is: 2008 The obtained month is: 8 The obtained date is: 8 When obtained: 20 The score obtained is: 8 The seconds obtained are: 8 ------------------------------------------------------- localDateTime = 2012-08-08T20:08:08 of = 2008-08-08T20:08:08 localDateTime1 = 2012-12-08T20:08:08 ------------------------------------------------------- localDateTime2 = 2012-12-10T20:08:08 localDateTime1 = 2012-12-08T20:08:08 localDateTime3 = 2012-12-10T23:08:08 ------------------------------------------------------- localDateTime4 = 2012-12-10T23:07:08 localDateTime3 = 2012-12-10T23:08:08 localDateTime5 = 2012-12-10T23:07:05
Overview of the Instant class
(1) Basic concepts
- The java.time.Instant class is mainly used to describe the instantaneous point in time information.
(2) Common methods
Method declaration | Function introduction |
---|---|
static Instant now() | Gets the current time from the system clock |
OffsetDateTime atOffset(ZoneOffset offset) | Combine this moment with the offset to create an offset date time |
static Instant ofEpochMilli(longepochMilli) | The object is constructed according to the number of milliseconds specified by the parameter. The parameter is the number of milliseconds from 0:0:0 on January 1, 1970 |
long toEpochMilli() | Gets the number of milliseconds from 0:0:0 on January 1, 1970 |
import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset; public class InstantTest { public static void main(String[] args) { // 1. Use the Instant class to obtain the current system time, which is not the default time zone of the current system. The difference between the original meridian and the East eight areas is 8 hours Instant now = Instant.now(); System.out.println("The current time obtained is:" + now); // 2. Add the 8 hours difference in time zone OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8)); System.out.println("The date and time after offset is:" + offsetDateTime); System.out.println("--------------------------------------------------------"); // 3. get the millisecond number of the current calling object from the standard base time. long g1 = now.toEpochMilli(); System.out.println("The obtained millisecond difference is:" + g1); // 4. Construct the object according to the number of milliseconds specified by the parameter Instant instant = Instant.ofEpochMilli(g1); System.out.println("The objects constructed according to the number of milliseconds specified in the parameter are:" + instant); } }
The current time obtained is 2021-09-18T17:25:30.194421700Z The date and time after offset is 2021-09-19T01:25:30.194421700+08:00 -------------------------------------------------------- The obtained millisecond difference is: 1631985930194 The object constructed according to the number of milliseconds specified in the parameter is 2021-09-18T17:25:30.194Z
Overview of DateTimeFormatter class
(1) Basic concepts
- The java.time.format.DateTimeFormatter class is mainly used to format and parse dates.
(2) Common methods
Method declaration | Function introduction |
---|---|
static DateTimeFormatter ofPattern(String pattern) | Gets the object according to the pattern specified by the parameter |
String format(TemporalAccessor temporal) | Converts the date and time specified by the parameter to a string |
TemporalAccessor parse(CharSequence text) | Converts the string specified by the parameter to a date time |
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAccessor; public class DateTimeFormatterTest { public static void main(String[] args) { // 1. Obtain the date and time of the current system and print it LocalDateTime now = LocalDateTime.now(); System.out.println("now = " + now); // 2. Prepare an object of DateTimeFormatter type according to the specified format DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 3. Realize the conversion from date and time to string type and print String str = dateTimeFormatter.format(now); System.out.println("The result of formatting adjustment is:" + str); // 4. Realize the conversion from string type to date time type and print TemporalAccessor parse = dateTimeFormatter.parse(str); System.out.println("The result of turning back is:" + parse); } }
The result of format adjustment is 2021-09-19 01:26:02 The result of turning back is:{},ISO resolved to 2021-09-19T01:26:02