Loading content...
Loading content...
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.
Before Java 8, the following classes were commonly used:
java.util.Date
java.sql.Date
java
Date date = new Date();
System.out.println(date);text
Thu Jul 17 10:30:45 IST 2025The Date class stores both date and time information.
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 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);java
17/07/2025java
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2022-12-20");
System.out.println(date);text
Tue Dec 20 00:00:00 IST 2022Not thread-safe
Mutable class
Complex to use
Can cause issues in multi-threaded applications
Java 8 introduced the java.time package to overcome the limitations of the old API.
Immutable
Thread-safe
Easy to use
Better API design
Supports date and time calculations
LocalDate represents only the date (year, month, day).
java
LocalDate date = LocalDate.now();
System.out.println(date);
date = date.plusDays(3);
System.out.println(date);java
boolean leapYear = date.isLeapYear();
System.out.println(leapYear);LocalTime represents only time.
java
LocalTime time = LocalTime.now();
System.out.println(time);
time = time.plusHours(2);
System.out.println(time);LocalDateTime represents both date and time.
java
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime);Period is used to calculate the difference between two dates.
java
Period period = Period.between(
LocalDate.parse("1991-05-20"),
LocalDate.now());
System.out.println(period);text
P34Y1M27DThis represents the difference in:
Years
Months
Days
Duration is used to calculate the difference between two times.
javaCopy
Duration duration = Duration.between(
LocalTime.parse("18:00"),
LocalTime.now());
System.out.println(duration);text
PT2H30M15SThis represents the difference in:
Hours
Minutes
Seconds
Old API
Mutable
Not Thread-Safe
Complex
New API
Immutable
Thread-Safe
Easy to Use
Built-in Date Calculations
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.
No, SimpleDateFormat is not thread-safe.
LocalDate
LocalTime
LocalDateTime
Period
Duration
Java 8 introduced the java.time package.
The new API is immutable and thread-safe.
LocalDate stores only the date.
LocalTime stores only the time.
LocalDateTime stores both date and time.
Period calculates differences between dates.
Duration calculates differences between times.
SimpleDateFormat is not thread-safe.
The new Date & Time API is preferred in modern Java applications.