Java Time API
Master the modern Date and Time API introduced in Java 8.
Java Time API
Prior to Java 8, date and time handling in Java was complex and error-prone. The new java.time package (JSR-310) provides a comprehensive and immutable API for date and time.
Key Classes
LocalDate: Represents a date without time (e.g.,2023-10-05).LocalTime: Represents a time without date (e.g.,14:30:00).LocalDateTime: Represents both date and time (e.g.,2023-10-05T14:30:00).ZonedDateTime: Represents a date and time with a time zone.Period: Represents a quantity of time in terms of years, months, and days.Duration: Represents a quantity of time in terms of seconds and nanoseconds.
Examples
Getting Current Date and Time
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
public class TimeExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("Date: " + date);
System.out.println("Time: " + time);
System.out.println("DateTime: " + dateTime);
}
}Tip 💡
The classes in java.time are immutable and thread-safe, making them much
safer to use than the old java.util.Date.
Formatting Dates
Use DateTimeFormatter to format dates.
import java.time.format.DateTimeFormatter;
LocalDateTime myDateObj = LocalDateTime.now();
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("Formatted Date: " + formattedDate);Period vs Duration
- Period: Measures time in years, months, days (compatible with
LocalDate). - Duration: Measures time in seconds, nanoseconds (compatible with
LocalTime,LocalDateTime).
LocalDate d1 = LocalDate.of(2020, 1, 1);
LocalDate d2 = LocalDate.of(2021, 1, 1);
Period p = Period.between(d1, d2); // 1 Year
LocalTime t1 = LocalTime.of(10, 0);
LocalTime t2 = LocalTime.of(12, 0);
Duration d = Duration.between(t1, t2); // 2 Hours