Sunday 19 June 2016

Date -Time API in Java

In this blog post we will explain both Java 8 & 7 Date -Time API.Java date time API has been changed in Java 8 with the introduction of a new set of packages & classes.The below figure showing the Java date-time API architecture.






















In Java 8, a new Date-Time API is introduced to cover the following disadvantages of Java 7 Date-time API

1-Thread safe - java.util.Date is not thread safe, thus developers have to deal with concurrency issue while using date. The new date-time API is immutable.

2-Date Time Format design - Default Date starts from 1900, month starts from 1, and day starts from 0, so no uniformity. The Java 7 Date -Time API had less direct methods for date operations.The new API provides numerous methods for such operations.

3-Different time zone handling -Developers had to write a lot of code to deal with time zone issues.The new Date-Time  API has been developed keeping zone-specific design in mind.



Java 8 Date -Time API:

The Java 8 new date time API is introduced in the Java package java.time which is part of the standard Java 8 API docs.

The java.time package also contains a set of subpackages which contains following below sub packages.
1-java.time.chrono 
2-java.time.class-use
3-java.time.format
4-java.time.temporal
5-java.time.zone

java.time.chrono contains classes to work with different countries calendars.
The java.time.format package contains classes used to parse and format dates from and to strings.


Following are the important classes introduced in java.time package ...

Local - Simplified date-time API with no complexity of time zone .
Zoned - Specialized date-time API to deal with various time zones.


1-Local Data-Time API

LocalDate/LocalTime and LocalDateTime classes simplify the development where time zones are not required.The below is demo example showing how to use Local Date-Time API.


Demo Example:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;

public class DateTime {

public static void main(String[] args) {

// Get the current date and time

LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current DateTime: " + currentDateTime);
LocalDate date = currentDateTime.toLocalDate();    // Get the local date From Current Local date time
System.out.println("date: " + date);
Month month = currentDateTime.getMonth();
int day = currentDateTime.getDayOfMonth();
int seconds =currentDateTime.getSecond();
System .out.println("Month: " + month +"day:" + day +"seconds: " + seconds);

LocalDateTime date1 = currentDateTime.withDayOfMonth(10).withYear(2016);
System .out.println("date1: " + date1);
//12 december 2016
LocalDate date2 = LocalDate.of(2016, Month.DECEMBER, 12);
System .out.println("date2: " + date2);
//20 hour 15 minutes
LocalTime date3 = LocalTime.of(20, 15);
System .out.println("date3: " + date3);
//parse a string
LocalTime date4 = LocalTime.parse("20:15:30");
System.out.println("date5: " + date4);

 
}

}

O/P Result:

Current DateTime: 2016-06-19T20:58:03.674
date: 2016-06-19
Month: JUNEday:19seconds: 3
date1: 2016-06-10T20:58:03.674
date2: 2016-12-12
date3: 20:15
date5: 20:15:30


2-Zoned Data-Time API

Zoned Date-Time API is to be used when time zoned is to be considered.Let see the below demo program.



import java.time.ZoneId;
import java.time.ZonedDateTime;

public class DateTime {

public static void main(String[] args) {

// Zone Date-Time API Testing


// Get the Zone Date Time
ZonedDateTime date = ZonedDateTime.parse("2016-06-19T10:15:30+05:30[Europe/Paris]");
System.out.println("date: " + date);
// Getting the current Zone ID
ZoneId currentZone = ZoneId.systemDefault();
System.out.println("CurrentZone: " + currentZone);

 
}

}

O/P Result:

date: 2016-12-03T10:15:30+01:00[Europe/Paris]
ZoneId: Europe/Paris
CurrentZone: Asia/Calcutta



Period & Duration:
In Java 8 Date-Time API introduced two most important deals with the time differences.

Period - It finds the period between date based time.
Duration - It finds the period between time based duration.



import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class DateTime {

public static void main(String[] args) {

//Test Duration
LocalDate date1 = LocalDate.now();
System.out.println("Current date:" + date1);
//add 2 months to the current date
LocalDate date2 = date1.plus(2, ChronoUnit.MONTHS);
System.out.println("Next month: " + date2);
Period period = Period.between(date2, date1);
System .out.println("Period: " + period);

// Test Period
LocalTime time1 = LocalTime.now();
Duration fourHours = Duration.ofHours(4);
LocalTime time2 = time1.plus(fourHours);
Duration duration = Duration.between(time1, time2);
System.out.println("Duration: " + duration);
 
}
}


O/P Result:
Current date:2016-06-19
Next month: 2016-08-19
Period: P-2M
Duration: PT-20H



The Java 8 date time API consists of the following classes:

Instant class:

Represents an instant in time on the time line.In Java 8 the Instant class represents an instant in time represented by a number of seconds and a number of nanoseconds since Jan. 1st 1970.

Duration Class:

Duration class represents a duration of time,for instance the time between two instants.

LocalDate Class:

Represents a date without time zone information.

LocalDateTime Class:

Represents a date and time without time zone information.

LocalTime Class:
Represents a local time of day without time zone information.


ZonedDateTime Class:
Represents a date and time including time zone information

DateTimeFormatter Class:
Formats date time objects as a Strings. For instance a ZonedDateTime or a local date time.

2 comments:

  1. it is very very beneficial for my java prepration thanks to yu sir

    ReplyDelete
  2. Thanks for a nice article. You have described it in a very impressive manner. I always love to read interesting and useful stuff. Java 7 & 8 features are nicely explained & useful for newbies like me. Sharing some additional knowledge on Java 8 API.

    ReplyDelete