Saturday, May 18, 2013

New Date & Time API in JDK8

The existing Java date and time classes are poor, mutable, and have unpredictable performance. There has been a long-standing desire for a better date and time API based on the Joda-Time project. The new API will have a more intuitive design allowing code to better express its intent. The classes will also be immutable which aligns with the multi-core direction of the industry.

package com.lambada.practice;

import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;

public class EarlyReleaseDemo {

 
 public static void main (String [] args) {
  new EarlyReleaseDemo().dataAndTimeAPI();
 }
 
 
 public void dataAndTimeAPI() {
  Clock clock = Clock.systemUTC();
  System.out.println("Current time in milisecond : " +clock.millis());
  
  LocalDate date = LocalDate.now();
  
  LocalTime localTime = LocalTime.now();

  
  
  ZoneId zoneId = ZoneId.systemDefault();
  
  //console output
  System.out.println("Current Time : " +localTime);
  System.out.println("Current Hour : " +localTime.getHour());
  System.out.println("Current Min. : " +localTime.getMinute());
  System.out.println("Current Second : " +localTime.getSecond());
  System.out.println("Todays date : " +date);
  System.out.println("Year : "+date.getYear());
  System.out.println("Month Value : "+date.getMonthValue());
  System.out.println("Month :"+date.getMonth());
  System.out.println("Day of Month :"+date.getDayOfMonth());
  System.out.println("Day of year : "+date.getDayOfYear());
  System.out.println("System default zone : " +zoneId.normalized());
  System.out.println("Available Zone Id's :" + zoneId.getAvailableZoneIds());

 }
}


//output
Current time in milisecond : 1368893133938
Current Time : 22:05:34.048
Current Hour : 22
Current Min. : 5
Current Second : 34
Todays date : 2013-05-18
Year : 2013
Month Value : 5
Month :MAY
Day of Month :18
Day of year : 138
System default zone : Asia/Almaty
Available Zone Id's :[America/Virgin, Europe/London, Atlantic/Reykjavik, Canada/Eastern, Africa/Ndjamena, America/Grenada, Asia/Saigon, Asia/Muscat, Europe/Sarajevo, America/Belem, Eire, Greenwich, Universal, Asia/Kashgar, ...........
Reference's : http://openjdk.java.net/jeps/150
http://ttux.net/post/java-8-new-features-release-performance-code/

No comments:

Post a Comment