Have you used the date and time API in JDK1.8?

reference answer

Why does the time and date API not work before JDK 1.8?

1. java.util.Date is provided from JDK 1.0, and the usability is poor

  • The default is Central Europe Time

  • Start year is 1900

  • start month starts at 0

  • Objects can be modified after creation

2. JDK 1.1 discarded many methods in Date, and added and recommended to use java.util.Calendar class

  • Compared to Date with the year removed starting from 1900

  • Months still start at 0

  • Choose Date or Calendar for more confusion

3. DateFormat formats time, thread is not safe

In order to solve the problem that time and date are difficult to use in JDK, JDK 1.8 has absorbed many functions of Joda-Time, added java.time package, and added new features:

  • Distinguish between human-readable and machine-friendly time and date classes

  • Objects related to date, time and comparison cannot be modified after they are created

  • Concurrent parsing and formatting of dates and times

  • Support setting different time zones and calendars

LocalDate Local Date
LocalTime Local Time
LocalDateTime Local Date + Time
Instant Time stamp, suitable for machine time calculation
Duration Time difference
Period Year, month, day difference
ZoneOffset Time zone offset
ZonedDateTime Date time with time zone
Clock Clock Get clocks in other regions
DateTimeFormatter Time format
Temporal Date-time field for obtaining values
TemporalAdjuster Emporal object conversion, realize custom
ChronoLocalDate Calendar system interface

Common APIs

1. Get the current date

LocalDate.now()

2. Creation date

LocalDate date = LocalDate.of(2020, 9, 21)

3. Get the year

date. getYear()

// Obtain the year through the implementation enumeration class ChronoField.YEAR of the TemporalField interface
date.get(ChronoField.YEAR)

4. Get the month

date.getMonth().getValue()

// Obtain the month through the implementation enumeration class ChronoField.MONTH_OF_YEAR of the TemporalField interface
date.get(ChronoField.MONTH_OF_YEAR)

5. Acquisition date

date.getDayOfMonth()

//Get the date through the implementation enumeration class ChronoField.DAY_OF_MONTH of the TemporalField interface
date.get(ChronoField.DAY_OF_MONTH)

6. Get the day of the week

date.getDayOfWeek()

7. Get the number of days in the current month

date. lengthOfMonth()

8. Get whether the current year is a leap year

date.isLeapYear()

9. Current time

LocalTime nowTime = LocalTime.now()

10. Creation time

LocalTime.of(23, 59, 59)

11. When obtaining

nowTime. getHour()

12. Get points

nowTime. getMinute()

13. Get seconds

nowTime. getSecond()

14. Get milliseconds

nowTime.getLong(ChronoField.MILLI_OF_SECOND)

15. Get nanoseconds

nowTime. getNano()

16. Create a datetime object

LocalDateTime.of(2020, 9, 21, 1, 2, 3);
LocalDateTime.of(date, nowTime);

17. Get the current date and time object

LocalDateTime.now()

18. Create a datetime object through LocalDate

date.atTime(1, 2, 3)

19. Create a datetime object through LocalTime

nowTime.atDate(date)

20. Get the LocalDate object through LocalDateTime

LocalDateTime.now().toLocalDate()

21. Get the LocalTime object through LocalDateTime

LocalDateTime.now().toLocalTime()

22. Parsing date strings

LocalDate. parse("2020-09-21")

23. Parsing time strings

LocalTime. parse("01:02:03")

24. Parsing date and time strings

LocalDateTime.parse("2020-09-21T01:02:03", DateTimeFormatter.ISO_LOCAL_DATE_TIME)

25. Time processing instant for convenient time modeling and machine processing, starting time 1970-01-01 00:00:00

//start time + 3 seconds
Instant.ofEpochSecond(3)
//start time + 3 seconds + 1 million nanoseconds
Instant.ofEpochSecond(3, 1_000_000_000)
//start time + 3 seconds - 1 million nanoseconds
Instant.ofEpochSecond(3, -1_000_000_000))
//The number of milliseconds from 1970-01-01 00:00:00
Instant.now().toEpochMilli()

26. Duration: Time difference processing of LocalTime, LocalDateTime, Intant

Duration.between(LocalTime.parse("01:02:03"), LocalTime.parse("02:03:04"))
Duration.between(LocalDateTime.parse("2020-09-21T01:02:03"), LocalDateTime.parse("2020-09-22T02:03:04"))
Duration.between(Instant.ofEpochMilli(1600623455080L), Instant.now())

27. Date time, before, after, equal comparison

//2020-09-21 before 2020-09-18?
LocalDate.parse("2020-09-21").isBefore(LocalDate.parse("2020-09-18"))
//01:02:03 after 02:03:04?
LocalTime.parse("01:02:03").isAfter(LocalTime.parse("02:03:04"))

28. Modify date and time objects and return a copy

//Modify the date and return a copy
LocalDate.now().withYear(2019).withMonth(9).withDayOfMonth(9)
LocalDate date4Cal = LocalDate.now();
//Add a week
date4Cal.plusWeeks(1)
// minus two months
date4Cal.minusMonths(2)
// minus three years
date4Cal.minusYears(3)

29. Formatting

//Format the current date
LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)
//Specify the format, format the current date
LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
Specify the format, format the current date and time
// format the current datetime
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss"))

30. Analysis

//date parsing
LocalDate. parse("2020-09-20")
// Specify format, date parsing
LocalDate.parse("2020/09/20", DateTimeFormatter.ofPattern("yyyy/MM/dd"))
//specified format, date time parsing
LocalDateTime.parse("2020/09/20 01:01:03", DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))

31. Time zone

//Shanghai time zone
ZoneId shanghaiZone = ZoneId.of("Asia/Shanghai");
//Set the date to Shanghai time zone
LocalDate.now().atStartOfDay(shanghaiZone)
//Set the date and time to Shanghai time zone
LocalDateTime.now().atZone(shanghaiZone)
//Set Instant to Shanghai time zone
Instant.now().atZone(shanghaiZone)

32. Meridian time difference

//Time difference minus 1 hour
ZoneOffset offset = ZoneOffset.of("-01:00");
//Set the time difference
OffsetDateTime.of(LocalDateTime.now(), offset)