Sub topic 5
Combining Date and Time with LocalDateTime
You already know LocalDate, which holds a calendar date, and LocalTime, which holds a time of day. Many real tasks need both at once. A dentist appointment, for example, happens on July 1 at 9:30. LocalDateTime is a class that holds a date and a time of day together in one object.
What LocalDateTime Holds
A LocalDateTime stores a year, month, day, hour, minute, second, and nanosecond. It does not store a time zone. The word "local" means "as shown on a wall clock and calendar somewhere, without saying where."
Creating and Reading a LocalDateTime
There are several ways to build one. You can pass all the numbers to of, join an existing date and time, or call atTime on a date.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class DateTimes {
public static void main(String[] args) {
LocalDateTime checkup = LocalDateTime.of(2024, 7, 1, 9, 30);
System.out.println(checkup); // 2024-07-01T09:30
LocalDate day = LocalDate.of(2024, 7, 1);
LocalTime time = LocalTime.of(14, 0);
LocalDateTime meeting = LocalDateTime.of(day, time);
System.out.println(meeting); // 2024-07-01T14:00
LocalDateTime lunch = day.atTime(12, 15);
System.out.println(lunch); // 2024-07-01T12:15
System.out.println(meeting.getHour()); // 14
System.out.println(meeting.getDayOfMonth()); // 1
System.out.println(meeting.toLocalDate()); // 2024-07-01
System.out.println(meeting.toLocalTime()); // 14:00
}
}The printed form uses a capital T to separate the date from the time. This layout comes from the ISO 8601 standard, a worldwide format for writing dates and times.
Changing Dates and Times
LocalDateTime is immutable like the other java.time types. Each change returns a new object, so you must keep the result. When you add time, the date rolls over for you.
import java.time.LocalDateTime;
public class Rollover {
public static void main(String[] args) {
LocalDateTime meeting = LocalDateTime.of(2024, 7, 1, 14, 0);
LocalDateTime late = meeting.plusHours(12);
System.out.println(late); // 2024-07-02T02:00
LocalDateTime moved = meeting.withHour(10).plusDays(3);
System.out.println(moved); // 2024-07-04T10:00
System.out.println(moved.isAfter(meeting)); // true
}
}Adding 12 hours to 2 p.m. moved the date to July 2. You did not have to work out the date change yourself.
When to Use It
| Need | Type to use |
|---|---|
| Only a date, such as a birthday | `LocalDate` |
| Only a time, such as a shop's opening hour | `LocalTime` |
| A date and time with no place attached | `LocalDateTime` |
Practice
Exercise
What does this line print? System.out.println(LocalDateTime.of(2024, 12, 31, 23, 0).plusMinutes(90));
Show answer
It prints 2025-01-01T00:30. Adding 90 minutes to 11:00 p.m. on December 31 passes midnight, so the day, month, and year all roll forward.
Exercise
You have LocalDate d = LocalDate.of(2025, 3, 3). Write one expression that gives a LocalDateTime for that date at 8:45 a.m.
Show answer
d.atTime(8, 45), which gives 2025-03-03T08:45. LocalDateTime.of(d, LocalTime.of(8, 45)) also works.