A brief introduction on java.time package introduced in Java 8

 



If you have ever worked with the java.util.Date data types for java web applications, some or the other time you might have faced the time zone issues.


Lets take the below example to understand one of the issue associated with this data type


You have a web application which the user needs to enter the date of birth in UI, and the backend API persists this into DB


Lets say User has born on June 15th 1978, 8:30PM in New Jersey, USA. lets say now the user is present in Japan his DOB is not going to change according to the Japan timezone. It is still the same fact that he was born on the date, time and location.


But if we use the java.util.Date data type for the DOB in java application, when ever the input date converts into Date data type in the backend java application java.util.Date will actually adds the timezone based on the location the application is running, for example in the above scenario if user sends the input as 1978-06-15T22:30:00 to the backend, and the backend application is running on Japan timezone our Date class will add timezone and when you access Date.toString will returns you the 1978-06-16T09:30:00 which means the DOB changed from June 15 to June 16th.


There are some workarounds we have to solve the above issue, before the java 8 we have third party libraries called Joda Time to solve the date and time issues associated with java.util.Date, but starting from java 8, new classes are introduced in java.time package which resolves so many issues associate with the java.util.Date.


All the classes in the java.time package that starts with Local** like LocalDate, LocalTime, LocalDateTime stores the date without adding the timezone


It has ZoneDateTime, Instant to deal with the dates with timezone.


Following are the brief introduction to the classes present in the java.time package.


Clock: provides the access to the current instant, date and time using the timezone

How to get Clock object

Clock.systemDefaultZone() — returns the clock representing the server timezone

Clock.systemUTC() — will return the clock representing the UTC timezone

Clock.system(ZoneId) — will return the clock with the provided zoneId

instant methods:

Clock.instant() — will return the Instant object

Clock.millis() — will return the milliseconds from the java epoch (since January 1st 1970)


Duration: represents the amount of time like for example 1 hour 


LocalDate: represents the date without time and timezone

LocalDate.now() — returns the LocalDate object with current date based on the system clock ex: 2022-06-22


LocalTime: represents the time without date and timezone

LocalTime.now() — returns the LocalTime object with the current time based on the system clock ex: 02:13:58.393902


LocalDateTime — represents the date and time without the timezone

LocalDateTime.now() — returns the LocalDateTime object ex: 2022-06-22T02:15:03.285727


ZonedDateTime: represents the Date and time based on the current timezone and also stores the timezone information

ZonedDateTime.now() — returns the current date and time with timezone info ex: “2022-06-22T02:15:28.132333-04:00[America/New_York]”


Instant: represents a single instantaneous point of the time line, for time and date it will query the system UTC clock.

Instant.now() — returns the Instant object with date and time with UTC timezone ex: 2022-06-22T06:20:19.775097Z


ZoneId: represents the Zone information based on the zone id string for a complete list of zone ids refer to ZoneId documentation at https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html#SHORT_IDS

ZonedDateTime.ofInstant(Instant.now(),ZoneId.of("Asia/Kolkata")) — will return the Instant with the mentioned timezone


Some of the points to remember about Classes in this package.


  1. LocalDate, LocalTime, LocalDateTime, ZonedDateTime etc. are final classes and cannot create subclasses
  2. Date classes are immutable and hence the state of the object cannot be modified
  3. Any modification to the date object will return a new object instead of modifying the state of own Object.
    1. Lets say 

      var curr = Instant.now();
      var currplus1 = curr.plus(Duration.ofHours(1))
      currplus1 will be new object and the state of curr object remains same

  4. Its better to go through the java.time.temporal, java.time.chrono, java.time.format, java.time.Zone package

Comments

Popular posts from this blog

How shared libraries work in ModuleFederationPlugin.

Single SPA using single-spa-react and ModuleFederationPlugin

Micro Frontend and implementation using single-spa-react framework and SystemJS