1.

New Date/Time API in Java 8

Answer»

Java 8 introduced a new Date/Time Application Program Interface(API) as the previous Date/Time API w drawbacks.

  • Not thread safe: The java.util.Date class was not thread safe. This FORCED developers to cope with the concurrency issue when the threads of the Date class were not in complete order with each other. The new date-time API is immutable and does not have setter methods.
  • Poor design: Default Date had become obsolete since it started from the year 1900, MONTH started from 1, and day started from 0. Thus uniformity couldn’t be achieved.  The old API had less immediate methods for the functions to be PERFORMED on date. The new API provides numerous useful methods for such operations.
  • Inconvenient handling of time zone:  The problems faced due the timezone needed huge amount of code written by a developer. The new API has been developed bearing  the domain-specific blueprint in mind.
  • The new API has been listed under the java.time package. Some of the significant classes under the java.time package are:
  • Local: The Local class is a streamlined Data/Time API  without the hodoo of timezone handling
  • Zoned: The Zoned class is a streamlined Data/Time API with consideration of timezones

Let us see an EXAMPLE of the Local class with the new Date/Time API:

import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.Month; public class Example {   public void checkDate()     {        LocalDateTime currentTime = LocalDateTime.now(); // computing local Date/Time        System.out.println("Present DateTime: " + currentTime);        LocalDate date = currentTime.toLocalDate();  // computing local Data        System.out.println("Present Local Date : " + date);     // computing current local time in hours, minutes and seconds        int second = currentTime.getSecond();           int minute =currentTime.getMinute();        int hour=currentTime.getHour();        System.out.println("Hour: " + hour +"|Minute: " + minute +"|seconds: " + second);       }   public STATIC void main(String args[])   {      Example obj = new Example();      obj.checkDate();   } }

The output is as follows:

$javac Example.java $java Example Present DateTime: 2018-12-13T18:39:24.730 Present Local Date : 2018-12-13 Hour: 18|Minute: 39|seconds: 24

Now, let us see an example of the Zoned class with the new Date/Time API:

import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Example {    public static void Zone()    {     LocalDateTime dt = LocalDateTime.now();     DateTimeFormatter format = DateTimeFormatter.ofPattern(" HH:mm:ss dd-MM-YYYY");       String fcd = dt.format(format); // stores the formatted current date     System.out.println("Present formatted Time and Date: "+fcd);       ZonedDateTime zone = ZonedDateTime.now();       System.out.println("The Present zone is "+zone.getZone());      }    public static void main(String[] args)      {     Zone();    } }

The output is as follows:

$javac Example.java $java Example Present formatted Time and Date:  19:24:52 13-12-2018 The Present zone is Etc/UTC


Discussion

No Comment Found