New Date/Time API (JSR310) in Java 8

Introduction

This article is all about the New Date/Time API, a new feature of Java 8. In this article we will go through some example that implement "classes" and "methods" of this API (JSR310). Before we start let's have a look at the API that contains "Packages" and "Classes".

Date/Time API (JSR310)

The new API is taken from 3 core ideas, in other words Immutable-value classes, Domain-driven design, Separation of chronologies. This Date/Time API (JSR310) contains "Packages" and "Classes". The name with their descriptions are given below.

Date/Time API Packages

Package Description
java.time This API package contain dates, times, duration and instants.
java.time.chrono This is the generic API package for the calendar system.
java.time.format This API package provides classes for printing and parsing dates and times.
java.time.temporal By using this API package we can access date and time using date time adjusters and fields and units.
java.time.zone This API package contains classes for zones and their rules.

Date/Time API Package's Classes

Class Description
LocalDate Shows only a date (without offset or zone)
LocalTime Shows only time (without offset or zone)
LocalDateTime Shows date and time (without offset or zone)
OffsetDate Shows a date with an offset such as +05:30
OffsetTime Shows time with an offset such as +05:30
OffsetDateTime Shows date and time with an offset such as +05:30
ZonedDateTime Shows date and time with offset and time zone
YearMonth Shows a year and month
MonthDay Shows month and day
Period Shows a defined time (such as 1 week)

Working with "LocalDate" class

Example: In this example first we import the "java.time.*" package then we create an object of the "LocalDate" class then by using the object we call the "now()" method and then print the date using the object.

  1. import java.time.*;  
  2. public class date_eg  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalDate d;//create object of "LocalDate" class  
  7.         d=LocalDate.now();// by using object call "now()" method   
  8.         //or  
  9.         //LocalDate d=LocalDate.now();  
  10.         System.out.println("");  
  11.         System.out.println("Date Format of Your System is :yyyy-mm-dd");  
  12.         System.out.println("Specific Date of Your System is:"+d);  
  13.     }  
  14. }  

 Output


Example: In this example we are using the "of(int, int, int)" method.

  1. import java.time.*;  
  2. public class date_eg1  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalDate d=LocalDate.of(2015, Month.JANUARY, 19);  
  7.         //or  
  8.         //LocalDate d=LocalDate.of(2015, 1, 19);  
  9.         System.out.println("");  
  10.         System.out.println("Specific Date of Your System is:"+d);  
  11.     }  
  12. }  

 Output


Example: In this example we are using the "ofYearDay(int, int)" method.

  1. import java.time.*;  
  2. public class date_eg2  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalDate d=LocalDate.ofYearDay(201599);  
  7.         System.out.println("");  
  8.         System.out.println("NintyNineth Day of 2015 is:"+d);  
  9.     }  
  10. }  

Output


Working with "LocalTime" class

Example: In this example we are using the "now()" method of the "LocalTime" class.

  1. import java.time.*;  
  2. public class date_eg3  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalTime t=LocalTime.now();  
  7.         System.out.println("");  
  8.         System.out.println("Time Format of Your System is :HH:MM:SS.msec");  
  9.         System.out.println("Current Time of Your System is:"+t);  
  10.     }  
  11. }  

Output


Example: In this example we are using the "of(int, int)" and "of(int, int, int)" methods of the "LocalTime" class.

  1. import java.time.*;  
  2. public class date_eg4  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalTime t=LocalTime.of(120);  
  7.         LocalTime t1=LocalTime.of(131510);  
  8.         System.out.println("");  
  9.         System.out.println("Specific Time without seconds is :"+t);  
  10.         System.out.println("");  
  11.         System.out.println("Specific Time with seconds is :"+t1);  
  12.     }  
  13. }  

 Output


Example: In this example we are using the "ofSecondOfDay(int)" method of the "LocalTime" class.

  1. import java.time.*;  
  2. public class date_eg5  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalTime t=LocalTime.ofSecondOfDay(23456);  
  7.         System.out.println("");  
  8.         System.out.println("Specific Time of Second Day According to your System is:"+t);  
  9.     }  
  10. }  

 Output


Example: In this example we are using the "now()" method of the "LocalDateTime" class.

  1. import java.time.*;  
  2. public class date_eg6  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalDateTime t=LocalDateTime.now();  
  7.         System.out.println("");  
  8.         System.out.println("Date&Time Format of Your System is :YYYY-MM-SS HH:MM:SS.msec");  
  9.         System.out.println("Current Date&Time of Your System is:"+t);  
  10.     }  
  11. }  

 Output


Working with "LocalDateTime" class

Example: In this example we are using the "of(int, int, int, int, int)" methods of the "LocalDateTime" class.

  1. import java.time.*;  
  2. public class date_eg7  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalDateTime t=LocalDateTime.of(20151191159);  
  7.         LocalDateTime t1=LocalDateTime.of(2015, Month.JANUARY, 291259);  
  8.         System.out.println("");  
  9.         System.out.println("Specific Date&Time of Your System is:"+t);  
  10.         System.out.println("Specific Date&Time of Your System is:"+t1);  
  11.     }  
  12. }  

 Output


Example: In this example we are using the "now(ZoneId.of(String))" & "now(Clock.systemUTC())" methods of the "LocalDateTime" class.

  1. import java.time.*;  
  2. public class date_eg8  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalTime t=LocalTime.now(ZoneId.of("America/Los_Angeles"));  
  7.         LocalTime t1=LocalTime.now(Clock.systemUTC());  
  8.         System.out.println("");  
  9.         System.out.println("Date&Time Format is : HH:MM:SS.msec");  
  10.         System.out.println("Specific Time Zone is:"+t);  
  11.         System.out.println("Current Time Zone of Your System is:"+t1);  
  12.     }  
  13. }  

 Output


Example: In this example we get most of the information of a given date, in other words the integer value and the name of the week, an integer value, Name, minimum number of days, maximum number of days of the month, integer value, Name, total number of days and Leap Year info of the year.

  1. import java.time.*;  
  2. public class date_eg9  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         LocalDate d=LocalDate.of(2015214);  
  7.         boolean isBefore=LocalDate.now().isBefore(d);  
  8.         //info of given week  
  9.         DayOfWeek dw=d.getDayOfWeek();  
  10.         int dwv=dw.getValue();  
  11.         String dn=dw.name();  
  12.         System.out.println("");  
  13.         System.out.println("Day Number of Specific Week is:"+dwv);  
  14.         System.out.println("Day Name of Specific Week is:"+dn);  
  15.         //info of given month  
  16.         Month m=d.getMonth();  
  17.         int mv=m.getValue();  
  18.         int minl=m.minLength();  
  19.         int maxl=m.maxLength();  
  20.         System.out.println("");  
  21.         System.out.println("Specific Month is:"+m);  
  22.         System.out.println("Specific Month in Numbers is:"+mv);  
  23.         System.out.println("Minimum Days of Specific Month is:"+minl);  
  24.         System.out.println("Maximum Days of Specific Month is:"+maxl);  
  25.         //info of given year  
  26.         boolean ly=d.isLeapYear();  
  27.         int y=d.getYear();  
  28.         int dy=d.getDayOfYear();  
  29.         int Ly=d.lengthOfYear();  
  30.         System.out.println("");  
  31.         System.out.println("Specific Year is Leap Year :"+ly);  
  32.         System.out.println("Specific Year is :"+y);  
  33.         System.out.println("Day of Specific Year is:"+dy);  
  34.         System.out.println("Total Days of Specific Year is:"+Ly);  
  35.     }  
  36. }  

Output

 

Example: In this example we use the "Year" and "LocalDate" classes.

  1. import java.time.*;  
  2. public class date_eg10  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         Year y=Year.now();  
  7.         boolean ly=y.isLeap();  
  8.         LocalDate d=Year.of(2015).atDay(77);  
  9.         LocalDate yest= LocalDate.now().minusDays(1);  
  10.         LocalDate tomm= LocalDate.now().plusDays(1);  
  11.         System.out.println("");  
  12.         System.out.println("Current Year is Leap Year :"+ly);  
  13.         System.out.println("Seventy Seventh Day of Current Year Year :"+d);  
  14.         System.out.println("Yesterday Day was :"+yest);  
  15.         System.out.println("Tomorrow will be :"+tomm);  
  16.     }  
  17. }  

 Output


Working with Time Zone

Example: In this example we are using the "ZoneId" class.

  1. import java.time.*;  
  2. import java.io.*;  
  3. import java.util.*;  
  4. public class date_eg11  
  5. {  
  6.     public static void main(String args[])  
  7.     {  
  8.         ZoneId z1=ZoneId.systemDefault();  
  9.         Set<String> all=ZoneId.getAvailableZoneIds();  
  10.         System.out.println("");  
  11.         System.out.println("Zone Id of Your System is:"+z1);  
  12.         System.out.println("List of all Zone Id:");  
  13.         System.out.println(all);  
  14.     }  
  15. }  

 Output


Working with Periods

Example: In this example we are using the "Period" class.

  1. import java.time.*;  
  2. public class date_eg12  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.           
  7.         LocalDate fd=LocalDate.of(2014627);  
  8.         LocalDate sd=LocalDate.of(20151029);  
  9.         Period p=Period.between(fd, sd);//using periods  
  10.         int d=p.getDays();  
  11.         int m=p.getMonths();  
  12.         int y=p.getYears();  
  13.         boolean b=p.isNegative();  
  14.         System.out.println("");  
  15.         System.out.println("Difference in Days is:"+d);  
  16.         System.out.println("Difference in Months is:"+m);  
  17.         System.out.println("Difference in Years is:"+y);  
  18.         System.out.println("Firs Date > Second Date:"+b);  
  19.     }  
  20. }  

 Output


Summary

The summary of the preceding article is that the new Date/Time API (JSR310) in Java 8 performs very well. We can implement it in many ways for various types of needs.

Up Next
    Ebook Download
    View all
    Learn
    View all