Java 8 – Date Api

Java 8 introduced a new date-time API to overcome some of the drawbacks of old date-time API :

What were those drawbacks ?

  1. Thread Safety – The Date and Calendar classes are not thread safe, leaving developers to deal with the headache of hard to debug concurrency issues and to write additional code to handle thread safety. On the contrary the new Date and Time APIs introduced in Java 8 are immutable and thread safe, thus taking that concurrency headache away from developers.
  2. ZonedDate and Time – Developers had to write additional logic to handle timezone logic with the old APIs, whereas with the new APIs, handling of timezone can be done with Local and ZonedDate/Time APIs.
  3. APIs Design and Ease of Understanding – The Date and Calendar APIs are poorly designed with inadequate methods to perform day-to-day operations. The new Date/Time APIs is ISO centric and follows consistent domain models for date, time, duration and periods. There are a wide variety of utility methods that support the commonest operations.

Java 8 under the package java.time introduced a new date-time API, most important classes among them are :
1. Local : Simplified date-time API with no complexity of timezone handling.
2. Zoned : Specialized date-time API to deal with various timezones.

1. LocalDate/LocatTime and LocalDateTime API : Use it when time zones are NOT required.

      
// Java code for LocalDate 
// LocalTime Function 
import java.time.*; 
import java.time.format.DateTimeFormatter; 
 
public class Date { 

public static void LocalDateTimeApi() 
{ 
 
  // the current date 
  LocalDate date = LocalDate.now(); 
  System.out.println("the current date is "+ 
                      date); 
 
 
  // the current time 
  LocalTime time = LocalTime.now(); 
  System.out.println("the current time is "+ 
                      time); 
     
 
  // will give us the current time and date 
  LocalDateTime current = LocalDateTime.now(); 
  System.out.println("current date and time : "+ 
                      current); 
 
 
  // to print in a particular format 
  DateTimeFormatter format =  
    DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");   
  
  String formatedDateTime = current.format(format);   
   
  System.out.println("in foramatted manner "+ 
                      formatedDateTime); 
 
 
  // printing months days and seconds 
  Month month = current.getMonth(); 
  int day = current.getDayOfMonth(); 
  int seconds = current.getSecond(); 
  System.out.println("Month : "+month+" day : "+ 
                      day+" seconds : "+seconds); 
 
  // printing some specified date 
  LocalDate date2 = LocalDate.of(1950,1,26); 
  System.out.println("the repulic day :"+date2); 
 
  // printing date with current time. 
  LocalDateTime specificDate =  
      current.withDayOfMonth(24).withYear(2016); 

  System.out.println("specfic date with "+ 
                     "current time : "+specificDate); 
} 

  // Driver code 
  public static void main(String[] args)  
  { 
      LocalDateTimeApi(); 
  } 
} 

Output:

the current date is 2018-04-09
the current time is 06:21:10.409
current date and time : 2018-04-09T06:21:10.410
in foramatted manner 09-04-2018 06:21:10
Month : APRIL day : 9 seconds : 10
the repulic day :1950-01-26
specfic date with current time : 2016-04-24T06:21:10.410

2. Zoned date-time API : Use it when time zones are to be considered.

      
// Java code for Zoned date-time API 
import java.time.LocalDateTime; 
import java.time.ZoneId; 
import java.time.ZonedDateTime; 
import java.time.format.DateTimeFormatter; 

public class Zone { 

// Function to get Zoned Date and Time 
public static void ZonedTimeAndDate() 
{ 
  LocalDateTime date = LocalDateTime.now(); 
  DateTimeFormatter format1 =  
    DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
    
  String formattedCurrentDate = date.format(format1); 
    
  System.out.println("formatted current Date and"+ 
                    " Time : "+formattedCurrentDate);  

  // to get the current zone 
  ZonedDateTime currentZone = ZonedDateTime.now();  
  System.out.println("the current zone is "+ 
                      currentZone.getZone());  

  // getting time zone of specific place 
  // we use withZoneSameInstant(): it is 
  // used to return a copy of this date-time  
  // with a different time-zone,   
  // retaining the instant. 
  ZoneId tokyo = ZoneId.of("Asia/Tokyo"); 

  ZonedDateTime tokyoZone = 
          currentZone.withZoneSameInstant(tokyo); 
                  
  System.out.println("tokyo time zone is " +  
                      tokyoZone); 

  DateTimeFormatter format =  
      DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
    
  String formatedDateTime = tokyoZone.format(format);  

  System.out.println("formatted tokyo time zone "+ 
                      formatedDateTime); 
    
} 
    
  // Driver code 
  public static void main(String[] args)  
  { 
        
      ZonedTimeAndDate(); 
        
  } 
} 

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

Summary

In this article we learnt about the Java 8 Date Time Apis and its examples.
Hope you liked the article !


Leave a Comment