1. Introduction
In Java, it’s common to handle dates and times separately, especially when these components come from different sources. However, there are situations where we need to combine a LocalDate (date) with a LocalTime (time) into a single LocalDateTime object for further processing.
In this tutorial, we’ll explore various ways to combine date and time from separate variables in Java. Moreover, we’ll use the Java 8+ java.time package, which provides an intuitive API for working with date and time values.
2. Understanding Date and Time in Java
Java 8 introduced three key classes for handling date and time: LocalDate, LocalTime, and LocalDateTime. Each class serves a distinct purpose:
- LocalDate represents a date without a time component, such as 2023-09-12. It’s ideal for scenarios where only the date is relevant.
- LocalTime represents a time without a date component, such as 14:30. This class is useful when only the time is needed.
- LocalDateTime combines date and time into a single object, like 2023-09-12T14:30. This class is used when both date and time need to be handled together.
To combine LocalDate and LocalTime, we need to merge these objects into a single LocalDateTime. Let’s examine different approaches for achieving this.
3. Using LocalDateTime.of() Method
The simplest way to combine a LocalDate and LocalTime is to use the LocalDateTime.of() method. This method accepts a LocalDate and LocalTime as parameters and returns a LocalDateTime object.
Let’s first set up the LocalDate, LocalTime, and expected LocalDateTime that we’ll use across our examples:
LocalDate date = LocalDate.of(2023, 9, 12);
LocalTime time = LocalTime.of(14, 30);
LocalDateTime expectedDateTime = LocalDateTime.parse("2023-09-12T14:30");
We initialize a specific date and time and then parse the expected LocalDateTime result that we will use for assertions.
Now, we can reuse these values in our first test. Let’s combine the date and time using LocalDateTime.of():
@Test
public void givenDateAndTime_whenUsingLocalDateTimeOf_thenCombined() {
LocalDateTime combinedDateTime = LocalDateTime.of(date, time);
assertEquals(expectedDateTime, combinedDateTime);
}
In this example, we use the LocalDateTime.of() method to combine the LocalDate and LocalTime into a LocalDateTime object.
The LocalDateTime.of() method effectively merges these components into a single object to represent date and time.
4. Using LocalDate.atTime() Method
Another approach is to call the atTime() method directly on a LocalDate instance. This method allows us to append a LocalTime to the LocalDate and receive a LocalDateTime:
@Test
public void givenDateAndTime_whenUsingAtTime_thenCombined() {
LocalDateTime combinedDateTime = date.atTime(time);
assertEquals(expectedDateTime, combinedDateTime);
}
In this case, we call date.atTime() to create a LocalDateTime instance from the LocalDate and LocalTime. This method provides a convenient way to merge date and time when we already have a LocalDate object.
5. Using TemporalAdjuster for Flexible Manipulation
Occasionally, we might need to manipulate the time before combining it with the date. For example, we can truncate the time down to the whole hour using a TemporalAdjuster:
@Test
public void givenDateAndTime_whenAdjustingTime_thenCombined() {
LocalDate date = LocalDate.of(2024, 9, 18);
LocalTime originalTime = LocalTime.of(14, 45);
LocalTime adjustedTime = originalTime.with(ChronoField.MINUTE_OF_HOUR, 0);
LocalDateTime expectedAdjustedDateTime = LocalDateTime.of(date, adjustedTime);
LocalDateTime actualDateTime = date.atTime(adjustedTime);
assertEquals(expectedAdjustedDateTime, actualDateTime);
}
In this example, we explicitly define the LocalDate as 2024-09-18 and the LocalTime as 14:45 (2:45 PM). We then use a TemporalAdjuster to set the minutes of the LocalTime to zero, truncating the time to the whole hour, resulting in 14:00 (2:00 PM).
After adjusting the time, we combine it with the date using LocalDateTime.of() to create an expected LocalDateTime. This is then compared to the result of date.atTime(), which should match the expected value, ensuring that the time manipulation and combination were successful.
6. Conclusion
Combining date and time in Java is straightforward using methods like LocalDateTime.of() and LocalDate.atTime(). These approaches allow us to merge separate LocalDate and LocalTime values into a unified LocalDateTime object for easier processing.
Additionally, we can use TemporalAdjusters to manipulate time before combining if more flexibility is required.
As always, the complete code samples for this article can be found over on GitHub.