Introduction to the Java Date and Time API in JDK 1.8

Posted By : Neha Yadav | 30-Sep-2019

Overview

New Date and Time API introduced with extra methods and thread-safety features in java 1.8.

New classes come in package java.time.* . Two important classes are Local (No timezone handling) and Zoned (deal with various timezones).

Some other classes are LocalDate, LocalTime, LocalDateTime, MonthDay, OffsetTime, OffsetDateTime, Clock, ZonedDateTime, ZoneId, ZoneOffset, Year, YearMonth, Period, Duration, Instant etc.

 

LocalDate :- It is immutable class and it represents date in default format i.e 'yyyy/mm/dd'. This class implements  ChronoLocalDate interface.

Example:-

LocalDate date = LocalDate.now();  

 

LocalTime :- It is immutable class and it represents time in default format i.e 'hh/mm/ss'. This class implements  Comparable interface. 

Example:-

LocalTime time = LocalTime.now();

System.out.println(time);   

 

LocalDateTime:It is an immutable class and it represents the date-time object in default format yyyy-MM-dd-HH-mm-ss.zzz. This class implements  ChronoLocalDateTime interface. 

Example:-

 
 
LocalDateTime now = LocalDateTime.now();    

 

MonthDay:- It is an immutable class and it represents the date-time objects in a combination of a month and day-of-month. This class implements  Comparable interface. 

Example:-

 
 
MonthDay month = MonthDay.now();    

LocalDate date = month.atYear(2019);   

System.out.println(date);      

 

OffsetTime:- It is an immutable class and it represents the date-time object in the form of time, as an hour-minute-second offset. This class implements Comparable interface.

Example:-

OffsetTime offset = OffsetTime.now();      

int h = offset.get(ChronoField.HOUR_OF_DAY);      System.out.println(h);     

int m = offset.get(ChronoField.MINUTE_OF_DAY);      System.out.println(m);      

int s = offset.get(ChronoField.SECOND_OF_DAY);      System.out.println(s);  

 

OffsetDateTime:- It is an immutable class and it represents a date-time object with an offset. This class implements Comparable interface.

Example:-

OffsetDateTime offsetDT = OffsetDateTime.now();       

System.out.println(offsetDT.getDayOfMonth());  

 

Clock:- This class is used to provide access to the current instant, date and time using a time zone.

Example:-

Clock c = Clock.systemDefaultZone();       

System.out.println(c.getZone());  

 

Year:- It is an immutable class which represents a date-time object with a year. It implements the Comparable interface.

Example:-

Year y = Year.now();    

System.out.println(y);   

 

Instant:- This class shows us the specific moment on the timeline. It implements the Comparable interface.

Example:-

Instant inst = Instant.parse("2019-09-30T10:37:30.00Z");      

System.out.println(inst);  

 

YearMonth:- This class is an immutable date-time object that represents the combination of a year and a month. It implements the Comparable interface.

Example:-

YearMonth ym = YearMonth.now();  

System.out.println(ym);  

Thanks.

About Author

Author Image
Neha Yadav

Neha is a creative person. She is having good knowledge of core java, advance java, hibernate,spring boot. She likes to solve puzzles, sudoku. She is a fun loving person.

Request for Proposal

Name is required

Comment is required

Sending message..