New Date/Time API (JSR310) in Java 8

Introduction

This article is about the New Date/Time API, a new feature of Java 8.  In this article we will go through some examples 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 names 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, we import the "java.time.*" package.   Then we create an object of the "LocalDate" class, by using the object we call the "now()" method. We then, print the date, using the object.
  1. import java.time.*;  
  2. public class date_eg {  
  3.  public static void main(String args[]) {  
  4.   LocalDate d;  
  5.   d = LocalDate.now();  
  6.   System.out.println("");  
  7.   System.out.println("Date Format of Your System is :yyyy-mm-dd");  
  8.   System.out.println("Specific Date of Your System is:" + d);  
  9.  }  
  10. }   
 Output
 
Example: In this example we are using the "of(int, int, int)" method.
  1. import java.time.*;  
  2. public class date_eg1 {  
  3.  public static void main(String args[]) {  
  4.   LocalDate d = LocalDate.of(2015, Month.JANUARY, 19);  
  5.   System.out.println("");  
  6.   System.out.println("Specific Date of Your System is:" + d);  
  7.  }  
  8. }   
 Output
 
Example: In this example we are using the "ofYearDay(int, int)" method.
  1. import java.time.*;  
  2. public class date_eg2 {  
  3.  public static void main(String args[]) {  
  4.   LocalDate d = LocalDate.ofYearDay(201599);  
  5.   System.out.println("");  
  6.   System.out.println("NintyNineth Day of 2015 is:" + d);  
  7.  }  
  8. }   
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.  public static void main(String args[]) {  
  4.   LocalTime t = LocalTime.now();  
  5.   System.out.println("");  
  6.   System.out.println("Time Format of Your System is :HH:MM:SS.msec");  
  7.   System.out.println("Current Time of Your System is:" + t);  
  8.  }  
  9. }   
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.  public static void main(String args[]) {  
  4.   LocalTime t = LocalTime.of(120);  
  5.   LocalTime t1 = LocalTime.of(131510);  
  6.   System.out.println("");  
  7.   System.out.println("Specific Time without seconds is :" + t);  
  8.   System.out.println("");  
  9.   System.out.println("Specific Time with seconds is :" + t1);  
  10.  }  
  11. }   
 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.  public static void main(String args[]) {  
  4.   LocalTime t = LocalTime.ofSecondOfDay(23456);  
  5.   System.out.println("");  
  6.   System.out.println("Specific Time of Second Day According to your System is:" + t);  
  7.  }  
  8. }   
 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.  public static void main(String args[]) {  
  4.   LocalDateTime t = LocalDateTime.now();  
  5.   System.out.println("");  
  6.   System.out.println("Date&Time Format of Your System is :YYYY-MM-SS HH:MM:SS.msec");  
  7.   System.out.println("Current Date&Time of Your System is:" + t);  
  8.  }  
  9. }   
 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.  public static void main(String args[]) {  
  4.   LocalDateTime t = LocalDateTime.of(20151191159);  
  5.   LocalDateTime t1 = LocalDateTime.of(2015, Month.JANUARY, 291259);  
  6.   System.out.println("");  
  7.   System.out.println("Specific Date&Time of Your System is:" + t);  
  8.   System.out.println("Specific Date&Time of Your System is:" + t1);  
  9.  }  
  10. }   
 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.  public static void main(String args[]) {  
  4.   LocalTime t = LocalTime.now(ZoneId.of("America/Los_Angeles"));  
  5.   LocalTime t1 = LocalTime.now(Clock.systemUTC());  
  6.   System.out.println("");  
  7.   System.out.println("Date&Time Format is : HH:MM:SS.msec");  
  8.   System.out.println("Specific Time Zone is:" + t);  
  9.   System.out.println("Current Time Zone of Your System is:" + t1);  
  10.  }  
  11. }   
 Output
 
Example: In this example we get most of the information, of a given date.  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.  public static void main(String args[]) {  
  4.   LocalDate d = LocalDate.of(2015214);  
  5.   boolean isBefore = LocalDate.now().isBefore(d);  
  6.   DayOfWeek dw = d.getDayOfWeek();  
  7.   int dwv = dw.getValue();  
  8.   String dn = dw.name();  
  9.   System.out.println("");  
  10.   System.out.println("Day Number of Specific Week is:" + dwv);  
  11.   System.out.println("Day Name of Specific Week is:" + dn);  
  12.   Month m = d.getMonth();  
  13.   int mv = m.getValue();  
  14.   int minl = m.minLength();  
  15.   int maxl = m.maxLength();  
  16.   System.out.println("");  
  17.   System.out.println("Specific Month is:" + m);  
  18.   System.out.println("Specific Month in Numbers is:" + mv);  
  19.   System.out.println("Minimum Days of Specific Month is:" + minl);  
  20.   System.out.println("Maximum Days of Specific Month is:" + maxl);  
  21.   boolean ly = d.isLeapYear();  
  22.   int y = d.getYear();  
  23.   int dy = d.getDayOfYear();  
  24.   int Ly = d.lengthOfYear();  
  25.   System.out.println("");  
  26.   System.out.println("Specific Year is Leap Year :" + ly);  
  27.   System.out.println("Specific Year is :" + y);  
  28.   System.out.println("Day of Specific Year is:" + dy);  
  29.   System.out.println("Total Days of Specific Year is:" + Ly);  
  30.  }  
  31. }   
Output
 
Example: In this example we use the "Year" and "LocalDate" classes.
  1. import java.time.*;  
  2. public class date_eg10 {  
  3.  public static void main(String args[]) {  
  4.   Year y = Year.now();  
  5.   boolean ly = y.isLeap();  
  6.   LocalDate d = Year.of(2015).atDay(77);  
  7.   LocalDate yest = LocalDate.now().minusDays(1);  
  8.   LocalDate tomm = LocalDate.now().plusDays(1);  
  9.   System.out.println("");  
  10.   System.out.println("Current Year is Leap Year :" + ly);  
  11.   System.out.println("Seventy Seventh Day of Current Year Year :" + d);  
  12.   System.out.println("Yesterday Day was :" + yest);  
  13.   System.out.println("Tomorrow will be :" + tomm);  
  14.  }  
  15. }   
 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.  public static void main(String args[]) {  
  6.   ZoneId z1 = ZoneId.systemDefault();  
  7.   Set < String > all = ZoneId.getAvailableZoneIds();  
  8.   System.out.println("");  
  9.   System.out.println("Zone Id of Your System is:" + z1);  
  10.   System.out.println("List of all Zone Id:");  
  11.   System.out.println(all);  
  12.  }  
  13. }   
 Output
 

Working with Periods


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