LocalDate and LocalTime are part of the Java 8 java.time API. They represent date and time independently, without timezone information. These classes are immutable, thread-safe, and far superior to the old Date and Calendar.
LocalDate → Date only (year, month, day)LocalTime → Time only (hour, minute, second)java.time packageClick the buttons to simulate Java method calls and see the result instantly.
These classes are created using static factory methods instead of constructors. Common methods include now(), of(), plusDays(), minusHours(), and getDayOfWeek().
// Importing LocalDate and LocalTime classes
import java.time.LocalDate;
import java.time.LocalTime;
public class LocalDateLocalTimeDemo {
public static void main(String[] args) {
// Getting current date
LocalDate today = LocalDate.now();
// Getting current time
LocalTime now = LocalTime.now();
System.out.println("Date: " + today);
System.out.println("Time: " + now);
}
}
The program fetches the system’s current date and time. Output will vary based on when the program is executed.
Sample Output:
Date: 2025-12-18
Time: 13:05:42.123
// Demonstrating date and time manipulation
LocalDate examDate = LocalDate.of(2025, 3, 10);
LocalTime examTime = LocalTime.of(10, 30);
// Adding days and hours
LocalDate newDate = examDate.plusDays(5);
LocalTime newTime = examTime.plusHours(2);
System.out.println(newDate);
System.out.println(newTime);
java.time API over legacy date classesLocalDate when time is irrelevantLocalTime for scheduling or time-only logicLocalDateTime when neededLocalDateLocalTime