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.
- import java.time.*;
- public class date_eg
- {
- public static void main(String args[])
- {
- LocalDate d;
- d=LocalDate.now();
-
-
- System.out.println("");
- System.out.println("Date Format of Your System is :yyyy-mm-dd");
- System.out.println("Specific Date of Your System is:"+d);
- }
- }
Output
Example: In this example we are using the "of(int, int, int)" method.
- import java.time.*;
- public class date_eg1
- {
- public static void main(String args[])
- {
- LocalDate d=LocalDate.of(2015, Month.JANUARY, 19);
-
-
- System.out.println("");
- System.out.println("Specific Date of Your System is:"+d);
- }
- }
Output
Example: In this example we are using the "ofYearDay(int, int)" method.
- import java.time.*;
- public class date_eg2
- {
- public static void main(String args[])
- {
- LocalDate d=LocalDate.ofYearDay(2015, 99);
- System.out.println("");
- System.out.println("NintyNineth Day of 2015 is:"+d);
- }
- }
Output
Working with "LocalTime" class
Example: In this example we are using the "now()" method of the "LocalTime" class.
- import java.time.*;
- public class date_eg3
- {
- public static void main(String args[])
- {
- LocalTime t=LocalTime.now();
- System.out.println("");
- System.out.println("Time Format of Your System is :HH:MM:SS.msec");
- System.out.println("Current Time of Your System is:"+t);
- }
- }
Output
Example: In this example we are using the "of(int, int)" and "of(int, int, int)" methods of the "LocalTime" class.
- import java.time.*;
- public class date_eg4
- {
- public static void main(String args[])
- {
- LocalTime t=LocalTime.of(12, 0);
- LocalTime t1=LocalTime.of(13, 15, 10);
- System.out.println("");
- System.out.println("Specific Time without seconds is :"+t);
- System.out.println("");
- System.out.println("Specific Time with seconds is :"+t1);
- }
- }
Output
Example: In this example we are using the "ofSecondOfDay(int)" method of the "LocalTime" class.
- import java.time.*;
- public class date_eg5
- {
- public static void main(String args[])
- {
- LocalTime t=LocalTime.ofSecondOfDay(23456);
- System.out.println("");
- System.out.println("Specific Time of Second Day According to your System is:"+t);
- }
- }
Output
Example: In this example we are using the "now()" method of the "LocalDateTime" class.
- import java.time.*;
- public class date_eg6
- {
- public static void main(String args[])
- {
- LocalDateTime t=LocalDateTime.now();
- System.out.println("");
- System.out.println("Date&Time Format of Your System is :YYYY-MM-SS HH:MM:SS.msec");
- System.out.println("Current Date&Time of Your System is:"+t);
- }
- }
Output
Working with "LocalDateTime" class
Example: In this example we are using the "of(int, int, int, int, int)" methods of the "LocalDateTime" class.
- import java.time.*;
- public class date_eg7
- {
- public static void main(String args[])
- {
- LocalDateTime t=LocalDateTime.of(2015, 1, 19, 11, 59);
- LocalDateTime t1=LocalDateTime.of(2015, Month.JANUARY, 29, 12, 59);
- System.out.println("");
- System.out.println("Specific Date&Time of Your System is:"+t);
- System.out.println("Specific Date&Time of Your System is:"+t1);
- }
- }
Output
Example: In this example we are using the "now(ZoneId.of(String))" & "now(Clock.systemUTC())" methods of the "LocalDateTime" class.
- import java.time.*;
- public class date_eg8
- {
- public static void main(String args[])
- {
- LocalTime t=LocalTime.now(ZoneId.of("America/Los_Angeles"));
- LocalTime t1=LocalTime.now(Clock.systemUTC());
- System.out.println("");
- System.out.println("Date&Time Format is : HH:MM:SS.msec");
- System.out.println("Specific Time Zone is:"+t);
- System.out.println("Current Time Zone of Your System is:"+t1);
- }
- }
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.
- import java.time.*;
- public class date_eg9
- {
- public static void main(String args[])
- {
- LocalDate d=LocalDate.of(2015, 2, 14);
- boolean isBefore=LocalDate.now().isBefore(d);
-
- DayOfWeek dw=d.getDayOfWeek();
- int dwv=dw.getValue();
- String dn=dw.name();
- System.out.println("");
- System.out.println("Day Number of Specific Week is:"+dwv);
- System.out.println("Day Name of Specific Week is:"+dn);
-
- Month m=d.getMonth();
- int mv=m.getValue();
- int minl=m.minLength();
- int maxl=m.maxLength();
- System.out.println("");
- System.out.println("Specific Month is:"+m);
- System.out.println("Specific Month in Numbers is:"+mv);
- System.out.println("Minimum Days of Specific Month is:"+minl);
- System.out.println("Maximum Days of Specific Month is:"+maxl);
-
- boolean ly=d.isLeapYear();
- int y=d.getYear();
- int dy=d.getDayOfYear();
- int Ly=d.lengthOfYear();
- System.out.println("");
- System.out.println("Specific Year is Leap Year :"+ly);
- System.out.println("Specific Year is :"+y);
- System.out.println("Day of Specific Year is:"+dy);
- System.out.println("Total Days of Specific Year is:"+Ly);
- }
- }
Output
Example: In this example we use the "Year" and "LocalDate" classes.
- import java.time.*;
- public class date_eg10
- {
- public static void main(String args[])
- {
- Year y=Year.now();
- boolean ly=y.isLeap();
- LocalDate d=Year.of(2015).atDay(77);
- LocalDate yest= LocalDate.now().minusDays(1);
- LocalDate tomm= LocalDate.now().plusDays(1);
- System.out.println("");
- System.out.println("Current Year is Leap Year :"+ly);
- System.out.println("Seventy Seventh Day of Current Year Year :"+d);
- System.out.println("Yesterday Day was :"+yest);
- System.out.println("Tomorrow will be :"+tomm);
- }
- }
Output
Working with Time Zone
Example: In this example we are using the "ZoneId" class.
- import java.time.*;
- import java.io.*;
- import java.util.*;
- public class date_eg11
- {
- public static void main(String args[])
- {
- ZoneId z1=ZoneId.systemDefault();
- Set<String> all=ZoneId.getAvailableZoneIds();
- System.out.println("");
- System.out.println("Zone Id of Your System is:"+z1);
- System.out.println("List of all Zone Id:");
- System.out.println(all);
- }
- }
Output
Working with Periods
Example: In this example we are using the "Period" class.
- import java.time.*;
- public class date_eg12
- {
- public static void main(String args[])
- {
-
- LocalDate fd=LocalDate.of(2014, 6, 27);
- LocalDate sd=LocalDate.of(2015, 10, 29);
- Period p=Period.between(fd, sd);
- int d=p.getDays();
- int m=p.getMonths();
- int y=p.getYears();
- boolean b=p.isNegative();
- System.out.println("");
- System.out.println("Difference in Days is:"+d);
- System.out.println("Difference in Months is:"+m);
- System.out.println("Difference in Years is:"+y);
- System.out.println("Firs Date > Second Date:"+b);
- }
- }
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.