Java 8 Date And Time

Date and Time in Java are very important for creating an application. Date and Time play a key role in so many applications like e-commerce, reservation of tickets, online banking, etc. So to manage date and time management easy Java 8 comes with a solution by Date and Time in Java API.

As we have already java.util.date what is thus the need for introducing a new Date-Time API in java 8.

There are the following reasons:-

  1. As java.util.date is not thread-safe and we cannot able to set date and time In Java.
  2. In java.util.date is very fewer methods to use.

Java LocalDate class

Java LocalDate class implements the chronoLocalDate Date interface. however, This uses the default format of yyyy-mm-dd. This class is immutable. We have to import java.time.LocalDate.

Method of Java LocalDate

MethodDescription
LocalDateTime at Time(int hour,int minute)This method thus combine date and time to create a LocalDateTime
int compareTo (ChronoLocalDate date)This method compares the given date with today date
boolean equals(Object date)however, Return true if both dates are the same otherwise Thus return false.
String format(DateTimeFormatter formatter)This method Thus format the date in a specified date format.
int get(TemporalField field)so it Return date as an integer value
boolean isLeapYear()This method returns true if the year is a leap year otherwise false.
LocalDate minusDays(long day)So, This method return date Thus after subtracting the specified number of days.
LocalDate minusMonths(long month)so, This method return date after subtracting the specified number of months.
static LocalDate now()This method returns Thus the current date in your local system 
LocalDate plusDays(long day)This method return date so after adding the specified days.
LocalDate plusMonths(long month)so, This method return date after adding the specified number of months.

Java LocalTime class 

Java LocalTime class inherits the Object class and implements the comparable interface.in order To use this class we have to import java.time.LocalTime. Jaca LocalTime class is an immutable class. however, This class uses the default time in the format as hour-minute-second.

MethodDescription
LocalDateTime atDate(Local date)This method combine date and time In Java to create a LocalDateTime
int compareTo (LocalTime )This method compares the given time with the specified time
boolean equals(Object time)Return true if both times is the same otherwise return false.
String format(DateTimeFormatter formatter)This method format the time in a specified date format.
int get(TemporalField field)Return time as an integer value
LocalTime minusHours(long H)This method returns time after subtracting the specified number of Hours.
LocalDate minusMinutes(long m)This method return date after subtracting the specified number of minutes.
static LocalTime now()This method returns the current time in your local system 
LocalDate plusHour(long H)This method returns time after adding the specified Hours.
LocalDate plusMinutes(long m)This method returns time after adding the specified number of minutes.

Java ZonedDate Time

If we creating an application which is worldwide so we have different time in a different country. although To solve this problem we have a java ZonedDate Time class. This class is also used to compare the time in different countries. hence, This class is an immutable representation of the time zone.

Methods of Java ZonedDate Time

MethodsDescription
String format(DateTimeFormatter format)This method return date-time in a specified format
int get(TemporalField field)This method return date-time in integer
ZoneId getZone()This return the date-time of the specified zone as “Asia/Kolkata”
ZonedDateTime withZoneSameInstant(ZoneId zone)This method is used to return dates and times with different time zones.
static ZonedDateTime now()Return current date and time.
static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone)It converts local time into zone date and time In Java
ZonedDateTime minus(long x, TemporalUnit unit)This return date-time after subtracting specified date and time
ZonedDateTime plus(long x, TemporalUnit unit)This return date-time after adding specified date and time In Java.

Conclusion of Date and time In Java

Date -time API introduced in java 8 make date and time thread-safe and increases the number of methods as compared to java.util.date. hence, For date, there is a local date class, for the time there is a local time class, and for date and time of different country zone there is zoned date and time In Java.

however, There are methods present to add and subtract from time, check for leap year, convert in different time zone, and also have date and time formats.

Written By: Charchil Gupta

Reviewed By: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *