Date and Time API

The Date and Time API in Java, introduced in Java 8, provides a comprehensive set of classes for working with dates, times, durations, and time zones. Prior to Java 8, the handling of date and time in Java was done using classes like Date and Calendar, which were not very user-friendly and had various limitations.

The new Date and Time API in Java 8 is part of the java.time package and is based on the ISO calendar system. Some key classes and interfaces in the Date and Time API include:

  1. LocalDate: Represents a date without a time component, such as "2022-01-15".

  2. LocalTime: Represents a time without a date component, such as "13:30:00".

  3. LocalDateTime: Represents a time without a date component, such as "2022-01-15T13:30:00".

  4. ZonedDateTime: Represents a date and time with a time zone, such as "2022-01-15T13:30:00+05:30".

  5. Duration: Represents a duration of time, such as "PT2H30M" (2 hours and 30 minutes).

  6. Period: Represents a period of time in terms of years, months, and days.

  7. DateTimeFormatter: Allows for formatting and parsing of dates and times.

The Date and Time API provides better support for handling time zones, daylight saving time, and leap years. It also offers immutability, thread-safety, and better readability compared to the older date and time classes.

Using the Date and Time API, you can perform various operations like adding or subtracting days, months, or years from a date, calculating differences between dates, formatting dates and times, parsing strings into date/time objects, and more.

Overall, the Date and Time API in Java provides a modern and powerful way to work with dates and times in Java applications. It is recommended to use this API for any new date and time-related development in Java projects.

In JDK1.8V Oracle team had introducted an API called "JODA-API".

\=>This API is developed by an organisation called "joda.org'' and it is present inside the package called "java.time" package.

JODA-API

a. LocalDate

Represents a date without a time component

Methods of LocalDate

public static java.time.LocalDate of(int, java.time.Month, int);

public static java.time.LocalDate of(int, int, int);

b. LocalTime

Represents a time without a date component

Methods of LocalTime

public static java.time.LocalTime of(int, int);

public static java.time.LocalTime of(int, int, int);

public static java.time.LocalTime of(int, int, int, int);

c. LocalDateTime

Represents a time without a date component

Methods of LocalDateTime

public static java.time.LocalDateTime of(int, java.time.Month, int, int, int);

public static java.time.LocalDateTime of(int, java.time.Month, int, int, int, int);

public static java.time.LocalDateTime of(int, java.time.Month, int, int, int, int, int);

public static java.time.LocalDateTime of(int, int, int, int, int);

public static java.time.LocalDateTime of(int, int, int, int, int, int);

public static java.time.LocalDateTime of(int, int, int, int, int, int, int);

EXAMPLE TO UNDERSTAND DATA AND TIME API

To create the date and time object as per our needs we use the following methods

import java.time.*;

public class Test{

public static void main(String[] args){

LocalDate ld = LocalDate.of(1993,Month.JANUARY,03);

System.out.println(ld);

LocalTime lt = LocalTime.of(19,45,35);

System.out.println(lt);

LocalDateTime ldt = LocalDateTime.of(1996,Month.FEBRUARY,24,9,30,45); System.out.println(ldt);

}

}

Output

1993-01-03

19:45:35

1996-02-24T09:30:45

0
Subscribe to my newsletter

Read articles from Anjan kumar Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anjan kumar Sharma
Anjan kumar Sharma

I am Java developer. I am seeking microservices in java . Life is always about creating and exploring yourself.