New feature of JDK8: date time related API
Problems with old version date time API:
- 1. Poor design: in java.tilogjava Sq| has date classes in its packages, java.util.Date Contains both date and time, and java.sql.Date Only. Contains the date. In addition, the classes used for formatting and parsing are java.text Defined in the package.
- 2. Non thread safety: java.util.Date It is non thread safe. All date classes are mutable, which is one of the biggest problems of Java date classes.
- 3. Time zone processing trouble: the date class does not provide internationalization, and there is no time zone support. So ava introduced java.util.Calendar And java.util.TimeZone Class, but they also have all of the above problems.
New date time API introduction
A new set of date time API has been added in JDK 8, which is reasonably designed and thread safe.
- java.time -Base package containing value objects
- java.time.chrono -Provide access to different calendar systems
- java.time.format -Format and parse time and date
- java.time.temporal -Includes underlying framework and extension features
- java.time.zone -Classes with time zone support
The new date and time API is located at java.time In the package, the following are some key classes:
- LocalDate: refers to the date, including MM DD YY. The format is 2019-10-16
- LocalTime: time, including time, minute and second, in the format of 16:38:54.158549300
- LocalDateTime: indicates the date time, including mm / DD / yyyy, HHM / s, in the format of 2018-09-06T15:33:56.750
- DateTimeFormatter: date time format class.
- Instant: time stamp, indicating a specific time instant.
- Duration: used to calculate the distance of 2 times (local time, hour minute second)
- Period: used to calculate the distance between two dates
- zonedDaterime: time with time zone
The calendar used in Java is the ISO 8601 calendar system, which is the world's civil calendar, which is what we call the Gregorian calendar. There are 365 days in a normal year and 366 in a leap year. In addition, ava 8 also provides four other calendars, namely:
- Thai buddhistdate: Buddhist calendar of Thailand
- MinguoDate: calendar of the Republic of China
- JapaneseDate: Japanese calendar
- HijrahDate: Islamic calendar
Common methods of LocalDateTime, LocalDate and LocalTime
LocalDateTime = LocalDate + LocalTime
(1.) get the current time of the default time zone
LocalDate localdate = LocalDate.now(); --Date of acquisition: LocalTime localtime = LocalTime.now(); --Get time: hours, minutes and seconds LocalDateTime localdatetime = LocalDateTime.now(); --Acquisition time: mm / DD / yyyy, HHM / S --result: localdate = 2020-06-16 localtime = 17:35:13.788 localdatetime = 2020-06-16T17:35:13.789
(2) Specify specific date and time
LocalDateTime dateTime = LocalDateTime.of(2089, 12, 25, 12, 30, 30); -- Mm / DD / yyyy HHM / S LocalDate date = LocalDate.of(2089, 12, 25); -- specific date LocalTime time1 = LocalTime.of(12, 30); -- time LocalTime time2 = LocalTime.of(12, 30, 30); -- Hour minute second LocalTime time3 = LocalTime.of(12, 30, 30, 150); -- Hour minute second nanosecond -- result dateTime: 2089-12-25T12:30:30 date: 2089-12-25 time1: 12:30 time2: 12:30:30 time3: 12:30:30.000000150
(3) Get specific month, day, hour, minute, second, week, day, etc
Here, LocalDate and LocalTime can be used separately or directly
LocalDate of = LocalDate.now(); System.out.println("year = " + of.getYear()); System.out.println("Month (English) = " + of.getMonth()); System.out.println("Month (number) = " + of.getMonthValue()); System.out.println("Day of the month = " + of.getDayOfMonth()); System.out.println("Day of the year = " + of.getDayOfYear()); System.out.println("week = " + of.getDayOfWeek()); LocalTime nowTime = LocalTime.now(); System.out.println("When:" + nowTime.getHour()); System.out.println("Points:" + nowTime.getMinute()); System.out.println("Seconds:" + nowTime.getSecond()); System.out.println("Nanosecond:" + nowTime.getNano());
(4) Date size judgment
The comparison between isBefore and isAfter here is the latter minus the former. If the latter is large, it returns a positive number, otherwise it is a negative number
LocalDate date1 = LocalDate.of(2020, 11, 30); LocalDate date2 = LocalDate.of(2089, 12, 25); System.out.println(date1.isBefore(date2)); System.out.println(date1.isAfter(date2)); System.out.println(date1.equals(date2)); System.out.println(date.isLeapYear()); -- Leap year or not
(5) How many days in a month or a year
System.out.println(date1.lengthOfMonth()); -- judge the number of days in this month System.out.println(date1.lengthOfYear()); -- judge the days of the year
(6) Directly modify, add, and reduce month, day, hour, minute and second
-- modify LocalDateTime dateTime1 = LocalDateTime.of(2089, 12, 25, 12, 30, 30); System.out.println("Set year:" + dateTime1.withYear(1991)); System.out.println("Set month:" + dateTime1.withMonth(5)); System.out.println("Set hours:" + dateTime1.withHour(3)); System.out.println("Set minutes:" + dateTime1.withMinute(25)); System.out.println("Set seconds:" + dateTime1.withSecond(26)); System.out.println("Set the day of the month:" + dateTime1.withDayOfMonth(20)); System.out.println("Set the day of the year:" + dateTime1.withDayOfYear(100)); -- increase System.out.println("Year added:" + dateTime1.plusYears(3)); System.out.println("Add month:" + dateTime1.plusMonths(5)); System.out.println("Add days:" + dateTime1.plusDays(3)); System.out.println("Add hours:" + dateTime1.plusHours(2)); System.out.println("Add minutes:" + dateTime1.plusMinutes(26)); System.out.println("Increase seconds:" + dateTime1.plusSeconds(20)); -- reduce System.out.println("Juvenile reduction:" + dateTime1.minusYears(3)); System.out.println("Decrease month:" + dateTime1.minusMonths(5)); System.out.println("Decrease days:" + dateTime1.minusDays(3)); System.out.println("Reduce hours:" + dateTime1.minusHours(2)); System.out.println("Reduce minutes:" + dateTime1.minusMinutes(26)); System.out.println("Reduce seconds:" + dateTime1.minusSeconds(20));
(7) Time type conversion
LocalDateTime dateTime1 = LocalDateTime.of(2089, 12, 25, 12, 30, 30); LocalDate date = LocalDate.of(2089, 12, 25); LocalTime time= LocalTime.of(12, 30, 30); System.out.println(dateTime1.toLocalTime()); -- LocalDateTime turn LocalTime System.out.println(dateTime1.toLocalDate()); -- LocalDateTime turn LocalDate System.out.println(date.atTime(time)); -- LocalDate and LocalTime turn LocalDateTime System.out.println(time.atDate(date)); -- LocalDate and LocalTime turn LocalDateTime -- result 12:30:30 2089-12-25 2089-12-25T12:30:30 2089-12-25T12:30:30
Date parsing and formatting: DateTimerFormatter
LocalDateTime dateTime = LocalDateTime.of(2089, 12, 25, 12, 30, 30); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); -- Both positive and negative can be called format method String date1 = dateTime.format(dtf); System.out.println(date1); String date2 = dtf.format(dateTime); System.out.println(date2); -- result 2089-12-25 12:30:30 2089-12-25 12:30:30
Calculate date time difference Duration and Period
- Duration
LocalDateTime today = LocalDateTime.now(); LocalDateTime dateTime = LocalDateTime.of(2089, 12, 25, 12, 30, 30); Duration duration = Duration.between(today, dateTime); System.out.println(duration.toDays()); -- Days of difference System.out.println(duration.toHours()); -- Hours of difference System.out.println(duration.toMinutes()); -- Minutes difference System.out.println(duration.toMillis()); -- Millisecond difference System.out.println(duration.toNanos()); -- Nanosecond difference
- Period
LocalDate today = LocalDate.now(); LocalDate date = LocalDate.of(2089, 12, 25); Period period = Period.between(today, date); System.out.println(period.getYears()); -- How many years is the difference System.out.println(period.getMonths()); -- How many months is the difference System.out.println(period.getDays()); -- How many days is the difference
Time adjusters: TemporalAdjusters and TemporalAdjusters
TemporalAdjuster is a functional interface, which can be customized;
TemporalAdjusters is a tool class for TemporalAdjusters, with many built-in methods
example:
-- Adjust to the first day of the next year LocalDateTime dateTime = LocalDateTime.of(2089, 12, 25, 12, 30, 30); LocalDateTime with = dateTime.with(TemporalAdjusters.firstDayOfNextYear()); System.out.println("with = " + with); -- result with = 2090-01-01T12:30:30
Time stamp: Instant
Instant instant = Instant.now(); -- Get the current time to 1970-01-01 00:00:00 MS value of System.out.println(instant); System.out.println("Seconds:" + instant .getEpochSecond()); System.out.println("Msec :" + instant .toEpochMilli());
there Instant.now() method uses UTC time by default instead of system default time. Click the source code to see
Time zone related: ZoneID, zonedatetime
--Get a collection of time zones in different parts of the world ZoneId.getAvailableZoneIds().forEach(System.out::println); ZonedDateTime now = ZonedDateTime.now();-- System default time zone ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());-- time zone UTC ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));--adopt ZoneId Specify time zone System.out.println("now = " + now); System.out.println("now1 = " + now1); System.out.println("now2 = " + now2);