You can use the atStartOfDay()
method from LocalDate
class to convert a LocalDate
into a LocalDateTime
. Then, you need to convert LocalDateTime
to a ZonedDateTime
using the atZone()
method.
Here is an example:
package org.kodejava.datetime;
import java.time.*;
public class LocalDateToZonedDateTimeExample {
public static void main(String[] args) {
// Create a LocalDate
LocalDate date = LocalDate.of(2023, Month.JULY, 9);
System.out.println("LocalDate: " + date);
// Convert LocalDate to LocalDateTime
LocalDateTime dateTime = date.atStartOfDay();
System.out.println("LocalDateTime: " + dateTime);
// Convert LocalDateTime to ZonedDateTime
ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
System.out.println("ZonedDateTime: " + zonedDateTime);
}
}
Output:
LocalDate: 2023-07-09
LocalDateTime: 2023-07-09T00:00
ZonedDateTime: 2023-07-09T00:00+08:00[Asia/Makassar]
In this example, we’re creating a LocalDate
for July 9, 2023. Then we’re converting it to a LocalDateTime
, and then to a ZonedDateTime
. The atStartOfDay()
method returns a LocalDateTime
set to the start of the day (00:00) on the date of this LocalDate
. The atZone()
method then takes the ZoneId
and returns a ZonedDateTime
representing the start of the day in that timezone.
The ZoneId.systemDefault()
returns the system default time zone. If you want to convert it to a specific time zone, you can specify the timezone as a string, like this: ZoneId.of("America/New_York")
.
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024