Java 8 - Convert LocalDate to ZonedDateTime
Asked Answered
C

4

25

I'm new to java.time package. I have a LocalDate 2015-12-10. I need to convert this to ZonedDateTime. Time should be 00:00:00 and Zone is ZoneOffset.UTC.

After conversion, ZonedDateTime should be 2015-12-10T00:00:00+02:00.

I'm storing the LocalDate in a variable called startDate.

I tried:

ZonedDateTime.ofInstant(Instant.from(startDate), ZoneOffset.UTC)

but get the error

java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: 2015-12-10 of type java.time.LocalDate]

I also tried:

startDate.atStartOfDay().atZone(ZoneOffset.UTC)

This gives unexpected results.

I looked at the API and tried a few other methods, but no luck so far.

Is there any other way to convert LocalDate to ZonedDateTime?

Crawly answered 14/8, 2015 at 3:14 Comment(3)
Why do you expect the zone offset to be +02:00?Nerveracking
If you want a ZonedDateTime, you need a ZoneId. Not just an ZoneOffset. And your question doesn't make much sense. First you say that you want it at the UTC time zone, and then you say it should be 2015-12-10T00:00:00+02:00. If it's +02:00, it's not UTC. UTC is +00:00. If what you want is a date time with a fixed UTC offset, you should use an OffsetDateTime: OffsetDateTime offsetDateTimeDateTime = date.atStartOfDay().atOffset(ZoneOffset.ofHours(2));Trevino
My apologies: ZoneOffset extends ZoneId, so that will return you a ZonedDateTime. The rest of my comment still stands though.Trevino
E
52

When converting less specific objects to more specific ones, use the 'at' methods:

ZonedDateTime zdt = startDate.atStartOfDay(ZoneOffset.UTC);

Passing in the UTC offset will not get a result of +02:00 however, which suggests you are trying to achieve something else.

Etti answered 14/8, 2015 at 10:13 Comment(0)
B
2

I supose the issue isn't relevant anymore but for google-purposes:

  LocalDate localDate = LocalDate.parse("2017-07-22");      
  ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
  // => 2017-07-22T00:00+05:30[Asia/Kolkata]

source https://beginnersbook.com/2017/10/java-convert-localdate-to-zoneddatetime/

Birl answered 30/7, 2018 at 14:14 Comment(0)
A
1

If you need to convert LocalDate to ZonedDateTime with current time you can use this one:

LocalDate localDate = LocalDate.parse("2017-07-22"); 
LocalDateTime localDateTime = localDate.atTime(LocalTime.now());
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault())
Abysm answered 22/6, 2022 at 11:29 Comment(0)
D
0
public ZonedDateTime transformToZonedDateTime(String Date, String timeZone) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    LocalDate localDate = LocalDate.parse(Date, formatter);
    LocalDateTime localDateTime = localDate.atTime(LocalTime.now());
    ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneOffset.UTC);
    ZoneId zoneId = ZoneId.of(timeZone);
    return zonedDateTime.withZoneSameInstant(zoneId);
}

INPUT: deliveryDate = "18-05-2023" and timeZone = "Asia/Hong_Kong"

RETURNS: "2023-05-19T20:43Z"

Dreeda answered 18/5, 2023 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.