Today’s sharing started, please give us more advice ~

This article will focus on the new date and time API.

New date and time API

1. The problem of the old date and time

In older versions the JDK was very bad with dates and times.

  • Java.util.Date contains both Date and time, while java.sql.Date contains only Date. Besides, the formatting and parsing classes are in the java.text package.

  • Non-thread-safe, java.util.Date is non-thread-safe, and all Date classes are mutable, which is one of the biggest problems with Java Date classes.

  • Time zone handling is cumbersome, date classes do not provide internationalization, there is no time zone support.

2. New date and time API introduction

JDK 8 adds a new set of date and time apis that are well designed and thread-safe. The new date and time API is in the java.time package, and here are some of the key classes.

  • LocalDate: indicates the date in the format of 2019-10-16

  • LocalTime: indicates the time, including the hour, minute and second, in the format of 16:38:54.158549300

  • LocalDateTime: indicates the date and time, including year, month, day, hour, minute, second, in the format of 2018-09-06T15:33:56.750

  • DateTimeFormatter: DateTimeFormatter class.

  • Instant: timestamp: indicates a specific Instant in time.

  • Duration: used to calculate the distance between two localtimes (minutes, seconds)

  • Period: calculates the distance between two dates (LocalDate, month, year, day)

  • ZonedDateTime: time including the time zone

The calendar used in Java is the ISO 8601 calendar system, which is the world’s civil calendar, known as the Gregorian calendar. An ordinary year has 365 days, and leap years have 366. Java 8 also provides four other calendars:

  • ThaiBuddhistDate: The ThaiBuddhist calendar

  • MinguoDate: Republic of China calendar

  • JapaneseDate: Japanese calendar

  • HijrahDate: Islamic calendar

2.1 Common operations on date and time

LocalDate, LocalTime, and LocalDateTime operations.

2.2 Modification and comparison of date and time

Note: The original LocalDate object is not modified when the date and time are changed. Each operation returns a new LocalDate object, so the data is safe in multi-threaded scenarios.

2.3 Formatting and Parsing operations

We can through the Java in JDK8. Time. The format. The DateTimeFormatter class to date parsing and formatting

2.4 Instant class

In JDK8 we have added an Instant class (timestamp/timeline) that internally stores seconds and nanoseconds from 00:00:00, January 1, 1970

2.5 Calculate the date time difference

Duration/Period two utility classes are provided in JDK8: calculate the time difference between dates

  1. Duration: used to calculate two LocalTime differences
  2. Period: used to calculate two localdates

2.6 Time corrector

Sometimes we need to adjust the date to “the first day of next month” and so on. It might be better if we go through the time corrector.

TemporalAdjuster: Time adjuster

TemporalAdjusters: Provides a number of commonly used TemporalAdjusters implementations through this static approach.

2.7 Time zone of date and time

Java8 supports time zones. LocalDate, LocalTime, and LocalDateTime do not contain time zones. The date and time classes with time zones are ZonedDate, ZonedTime, and ZonedDateTime.

Each time zone corresponds to an ID in the format of Region/city. For example, Asia/Shanghai.

ZoneId: This class contains all time zone information

Benefits of the JDK’s new date and time API:

  • In the new version of the datetime API, date and time objects are immutable, and manipulating dates does not affect the original value, but instead generates a new instance
  • Provide two different ways to effectively distinguish the operation of human and machine
  • TemporalAdjuster allows for more precise operation dates and customizable date adjustment periods
  • Thread safety

~ well, the new Date and time API is really useful, quickly replace the old Date!!

Today’s share has ended, please forgive and give advice!