VOOZH about

URL: https://www.geeksforgeeks.org/java/new-date-time-api-java8/

⇱ New Date-Time API in Java 8 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

New Date-Time API in Java 8

Last Updated : 8 Sep, 2025

Java 8 introduced a brand-new Date and Time API under the package java.time to overcome the limitations of the old java.util.Date and java.util.Calendar classes.

Why New Date-Time API?

  1. Not thread safe: Unlike old java.util.Date, which is not thread safe, the new date-time API is immutable and doesn't have setter methods.
  2. Fewer operations: In the old API, there are only a few date operations, but the new API provides us with many date operations.
  3. Confusing design: Mixing of date, time and timezone handling.

Core Classes in java.time

  • Local API: LocalDate, LocalTime, LocalDateTime (when timezone is not required).
  • Zoned API: ZonedDateTime, ZoneId (when working with timezones).
  • Period and Duration: Represent date-based and time-based amounts of time.
  • ChronoUnit Enum: Replace integer constants with type-safe units like DAYS, WEEKS, YEARS.
  • TemporalAdjusters: Utility for common date manipulations (like first day of month, next Saturday).

Example 1: LocalDate, LocalTime, LocalDateTime


Output
the current date is 2021-09-23
the current time is 20:52:39.954238
current date and time : 2021-09-23T20:52:39.956909
in formatted manner 23-09-2021 20:52:39
Month : SEPTEMBER day : 23 seconds : 39
the republic day :1950-01-26
specific date with current time : 2016-09-24T20:52:39.956909

Example 2: Zoned Date-Time API


Output
formatted current Date and Time : 09-04-2018 06:21:13
the current zone is Etc/UTC
tokyo time zone is 2018-04-09T15:21:13.220+09:00[Asia/Tokyo]
formatted tokyo time zone 09-04-2018 15:21:13

Example 3: Period and Duration


Output
gap between dates is a period of P6Y6M25D
the current time is 18:34:24.813548
after adding five hours of duration 23:34:24.813548
duration gap between time1 & time2 is PT-5H

Example 4: ChronoUnit Enum


Output
current date is :2018-04-09
next to next year is 2020-04-09
the next month is 2018-05-09
next week is 2018-04-16
20 years after today 2038-04-09

Example 5: TemporalAdjuster


Output
the current date is 2021-07-09
firstDayOfNextMonth : 2021-08-01
next saturday from now is 2021-07-10
firstDayOfMonth : 2021-07-01
lastDayOfMonth : 2021-07-31
Comment
Article Tags: