LocalDateTime.now() / LocalTime.now()
LocalDateTime is a class that represents both date and time, while LocalTime represents time only (Java 8+). Because neither class holds timezone information, they are used for tasks such as getting the server's local time or recording log timestamps.
Syntax
// Gets the current date and time. LocalDateTime now = LocalDateTime.now(); // Gets the current time. LocalTime time = LocalTime.now(); // Gets the hour (0–23). now.getHour(); // Gets the minute (0–59). now.getMinute(); // Gets the second (0–59). now.getSecond();
Method List
| Method | Description |
|---|---|
| LocalDateTime.now() | Returns the current date and time. |
| LocalDateTime.of(date, time) | Creates a LocalDateTime by combining a LocalDate and a LocalTime. |
| LocalTime.now() | Returns the current time. |
| LocalTime.of(hour, minute) | Creates a LocalTime with the specified hour and minute. |
| getHour() | Returns the hour as an int in the range 0–23. |
| getMinute() | Returns the minute as an int in the range 0–59. |
| getSecond() | Returns the second as an int in the range 0–59. |
| toLocalDate() | Extracts the date portion of a LocalDateTime as a LocalDate. |
| toLocalTime() | Extracts the time portion of a LocalDateTime as a LocalTime. |
Sample Code
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; // Gets the current date and time. LocalDateTime now = LocalDateTime.now(); System.out.println(now); // e.g., prints "2025-04-01T14:30:00.123456" // Gets each component of the date and time. System.out.println(now.getYear()); // Prints "2025" System.out.println(now.getMonthValue()); // Prints the month (1–12) System.out.println(now.getDayOfMonth()); // Prints the day of the month System.out.println(now.getHour()); // Prints the hour (0–23) System.out.println(now.getMinute()); // Prints the minute (0–59) // Gets the current time only. LocalTime time = LocalTime.now(); System.out.println(time); // e.g., prints "14:30:00.123456" // Creates a specific time. LocalTime lunch = LocalTime.of(12, 0); System.out.println(lunch); // Prints "12:00" // Creates a LocalDateTime by combining a LocalDate and a LocalTime. LocalDate date = LocalDate.of(2025, 4, 1); LocalTime t = LocalTime.of(9, 0); LocalDateTime meeting = LocalDateTime.of(date, t); System.out.println(meeting); // Prints "2025-04-01T09:00" // Extracts the date and time portions separately. System.out.println(now.toLocalDate()); // Prints the date only System.out.println(now.toLocalTime()); // Prints the time only
Notes
LocalDateTime is also immutable — methods such as plusHours() and minusMinutes() return a new object rather than modifying the original. If you need to account for timezones, use ZonedDateTime instead. Because LocalDateTime does not hold timezone information, it cannot accurately compare times across different timezones.
For formatting date-time values as strings, see DateTimeFormatter.ofPattern() / format().
If you find any errors or copyright issues, please contact us.