Date and Time API In Java Tutorials
Programming
Java Date and Time API
Java provides APIs to work with dates and times. Before Java 8, developers used the old Date and Time API. Java 8 introduced a new and improved Date and Time API in the java.time package.
Old Date & Time API
Before Java 8, the following classes were commonly used:
java.util.Datejava.sql.Date
Example
java
Date date = new Date();
System.out.println(date);Output
text
Thu Jul 17 10:30:45 IST 2025The Date class stores both date and time information.
Limitations of Old API
The old Date and Time API had several drawbacks:
Date and time are combined in a single object.
Classes are mutable.
Formatting is complicated.
Not thread-safe.
Difficult to work with date calculations.
SimpleDateFormat
SimpleDateFormat was used to format and parse dates.
Convert Date to String
java
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = sdf.format(new Date());
System.out.println(formattedDate);Output
java
17/07/2025Convert String to Date
java
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2022-12-20");
System.out.println(date);Output
text
Tue Dec 20 00:00:00 IST 2022Problems with SimpleDateFormat
Not thread-safe
Mutable class
Complex to use
Can cause issues in multi-threaded applications
Java 8 New Date & Time API
Java 8 introduced the java.time package to overcome the limitations of the old API.
Advantages
Immutable
Thread-safe
Easy to use
Better API design
Supports date and time calculations
LocalDate (Date Only)
LocalDate represents only the date (year, month, day).
Example
java
LocalDate date = LocalDate.now();
System.out.println(date);
date = date.plusDays(3);
System.out.println(date);Check Leap Year
java
boolean leapYear = date.isLeapYear();
System.out.println(leapYear);LocalTime (Time Only)
LocalTime represents only time.
Example
java
LocalTime time = LocalTime.now();
System.out.println(time);
time = time.plusHours(2);
System.out.println(time);LocalDateTime (Date and Time)
LocalDateTime represents both date and time.
Example
java
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime);Period (Date Difference)
Period is used to calculate the difference between two dates.
Example
java
Period period = Period.between(
LocalDate.parse("1991-05-20"),
LocalDate.now());
System.out.println(period);Output
text
P34Y1M27DThis represents the difference in:
Years
Months
Days
Duration (Time Difference)
Duration is used to calculate the difference between two times.
Example
javaCopy
Duration duration = Duration.between(
LocalTime.parse("18:00"),
LocalTime.now());
System.out.println(duration);Output
text
PT2H30M15SThis represents the difference in:
Hours
Minutes
Seconds
Old API vs New API
Old API
Mutable
Not Thread-Safe
Complex
New API
Immutable
Thread-Safe
Easy to Use
Built-in Date Calculations
Interview Questions
Why was the new Date and Time API introduced in Java 8?
The old API was mutable, not thread-safe, and difficult to use. Java 8 introduced the java.time package to provide a simpler, immutable, and thread-safe solution.
Is SimpleDateFormat thread-safe?
No, SimpleDateFormat is not thread-safe.
Which classes are commonly used in the Java 8 Date & Time API?
LocalDate
LocalTime
LocalDateTime
Period
Duration
Key Points
Java 8 introduced the
java.timepackage.The new API is immutable and thread-safe.
LocalDatestores only the date.LocalTimestores only the time.LocalDateTimestores both date and time.Periodcalculates differences between dates.Durationcalculates differences between times.SimpleDateFormatis not thread-safe.The new Date & Time API is preferred in modern Java applications.