Lesson 54
Date and Time API
The java.time package provides modern immutable classes — LocalDate, LocalTime, LocalDateTime, and DateTimeFormatter — for working with dates and times in Java.
Java Track
Save progress as you learn.
61 lessons
Lesson content
The Java Date and Time API provides modern classes for working with dates, times, and date-time values. Introduced in the java.time package, it offers a simple and efficient way to create, manipulate, format, and display date and time information.
Definition List
- LocalDate: Represents a date without time information.
- LocalTime: Represents a time without date information.
- LocalDateTime: Represents both date and time.
- DateTimeFormatter: Used to format and parse date-time values.
- now(): Returns the current system date or time.
- ofPattern(): Defines a custom date-time format pattern.
Date and Time Class Formats
LocalDate → yyyy-MM-dd e.g. 2026-06-23
LocalTime → HH:mm:ss.nnnnnnnnn e.g. 14:30:45.123456789
LocalDateTime → yyyy-MM-ddTHH:mm:ss.nnnnnnnnn e.g. 2026-06-23T14:30:45.123456789Example 01: Display Current Date (LocalDate)
package com.java.DataAndTime;
import java.time.LocalDate; // import the LocalDate class
public class Current_date {
public static void main(String[] args) {
LocalDate myObj = LocalDate.now(); // Create a date object
System.out.println(myObj); // Display the current date
}
}Output
2026-06-23Example 02: Display Current Time (LocalTime)
package com.java.DataAndTime;
import java.time.LocalTime; // import the LocalTime class
public class Current_time {
public static void main(String[] args) {
LocalTime myObj = LocalTime.now();
System.out.println(myObj);
}
}Output
14:30:45.123456789Example 03: Display Current Date and Time (LocalDateTime)
package com.java.DataAndTime;
import java.time.LocalDateTime; // import the LocalDateTime class
public class Current_date_Time {
public static void main(String[] args) {
LocalDateTime myObj = LocalDateTime.now();
System.out.println(myObj);
}
}Output
2026-06-23T14:30:45.123456789Example 04: Formatting Date and Time (DateTimeFormatter)
package com.java.DataAndTime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Formatting_Date_Time {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before formatting: " + myDateObj);
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting: " + formattedDate);
}
}Output
Before formatting: 2026-06-23T14:30:45.123456789
After formatting: 23-06-2026 14:30:45Common DateTimeFormatter Methods
// Get current date / time / date-time
LocalDate.now() // current date
LocalTime.now() // current time
LocalDateTime.now() // current date and time
// Define a custom format pattern
DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
// Apply the format to a date-time object
String formatted = myDateObj.format(myFormatObj);Best Practices
- Use java.time classes — prefer LocalDate, LocalTime, and LocalDateTime over legacy Date and Calendar classes.
- Use DateTimeFormatter for formatting — avoid manually constructing date strings.
- Choose the appropriate class: LocalDate for dates only, LocalTime for times only, and LocalDateTime for both.
- Use meaningful date formats that are easy for users to read and understand.
FAQ
1. What is the Java Date and Time API?
The Java Date and Time API is a collection of classes in the java.time package used to work with dates, times, and date-time values.
2. What is the difference between LocalDate and LocalDateTime?
LocalDate stores only date information, while LocalDateTime stores both date and time information.
3. How do I format a date in Java?
Use the DateTimeFormatter class with the ofPattern() method to define a format, then call format() on the date-time object.
Where Java is used
Java in real-world development
LocalDate
Represents a date (year, month, day) with no time component. Default format: yyyy-MM-dd.
LocalTime
Represents a time (hour, minute, second, nanosecond) with no date component. Default format: HH:mm:ss.nnnnnnnnn.
LocalDateTime
Combines date and time into a single object. Default format: yyyy-MM-ddTHH:mm:ss.nnnnnnnnn.
DateTimeFormatter
Formats and parses date-time objects using patterns defined with ofPattern().
now()
Static method available on LocalDate, LocalTime, and LocalDateTime that returns the current system value.
ofPattern()
Defines a custom date-time format string (e.g. "dd-MM-yyyy HH:mm:ss") for use with DateTimeFormatter.