0

As per the requirement, my code is supposed to append date from a ZonedDateTime parameter, and Time from OffSetTime parameter into this format, "yyyy-MM-dd HH:mm:ss.SSSz". However, i was not been able to achieve this

I have tried various ways , including the one below, using DateTimeFormatter.

ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-05-23T09:00:00-05:00");
OffsetTime offsetTime = OffsetTime.parse("08:59:00-05:00");
LocalDateTime localDateTime = LocalDateTime.of(zonedDateTime.toLocalDate(), offsetTime.toLocalTime());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSz");
String finalDate = localDateTime.format(formatter);

I notice that : - code is throwing "java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime" at localDateTime.format(formatter)

The expectation is to get the DateTime in String like so - "2019-05-23T08:59:00.000Z"

Any help appreciated, Thank you for your time.

Sahil
  • 159
  • 1
  • 12
  • A `LocalDateTime` doesn't have a time zone - it's not clear to me why you're using a `LocalDateTime` at all here... could you clarify how you want to combine `ZonedDateTime` and an `OffsetTime` anyway, in that they could have different offsets. (They don't in your example, but...) – Jon Skeet May 13 '19 at 19:48
  • I had to append zonedDateTime and offsetTime to a DateTime format, and i achieved it in the line `LocalDateTime.of(zonedDateTime.toLocalDate(), offsetTime.toLocalTime());` I don't care about localDateTime if i am able to parse it directly to the expected format yyyy-MM-dd HH:mm:ss.SSSz – Sahil May 13 '19 at 19:52
  • 1
    That's the code, but does it accurately represent what you want to do? If you had a ZonedDateTime with an offset of UTC-8, and an OffsetTime with an offset of UTC+12, would that definitely still work? And why are you then formatting it to include a time zone, when there's no time zone associated with a LocalDateTime? You've given an example using Z, but that means UTC, which isn't the offset for *either* the date or the time. – Jon Skeet May 13 '19 at 19:54
  • sorry, i have edited over my previous comment to include more context. Also, for the moment i am a 100% sure that the offsetTime and zonedDateTime is going to be having the same TimeZone. I know its not a clean solution, and i am open to suggestions. – Sahil May 13 '19 at 20:01
  • Okay - there's still the problem that you've started with offsets of UTC-5 and you're now ending up at UTC, which looks *really* unlikely to be a good idea. – Jon Skeet May 13 '19 at 20:17
  • Sorry, i was careless not to realize offsetTime.toLocalTime() do not consider the offset while resolving to localTime (that was how i thought toLocalTime works) . Andreas's solution helped me. – Sahil May 14 '19 at 03:40

1 Answers1

2

Four issues:

  • LocalDateTime doesn't have any time zone information, so don't use it.

  • Even if it did, your inputs only have a time zone offset, not a full time zone, so you format string cannot use z, as that requires a time zone name. Use XXX instead.

  • Since the inputs are offset -05:00 you shouldn't expect output with Z (Zulu), as that means +00:00, unless you also expect the time to be adjusted by 5 hours.

  • The format pattern for year should use uuuu, not yyyy. See uuuu versus yyyy in DateTimeFormatter formatting pattern codes in Java?

Assuming you want to keep the time zone, change code to:

ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-05-23T09:00:00-05:00");
OffsetTime offsetTime = OffsetTime.parse("08:59:00-05:00");
OffsetDateTime offsetDateTime = zonedDateTime.toLocalDate().atTime(offsetTime);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSXXX");
System.out.println(offsetDateTime.format(formatter));
2019-05-23 08:59:00.000-05:00

If you instead want Zulu time, i.e. UTC, with the time adjusted, add the line to make the adjustment:

OffsetDateTime offsetDateTime = zonedDateTime.toLocalDate().atTime(offsetTime)
                                .withOffsetSameInstant(ZoneOffset.UTC);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSXXX");
System.out.println(offsetDateTime.format(formatter));
2019-05-23 13:59:00.000Z
Andreas
  • 138,167
  • 8
  • 112
  • 195
  • 1
    Very knowledgeable, thank you. I might include a warning in case the two offsets provided disagree. And I might find `OffsetDateTime offsetDateTime = zonedDateTime.toLocalDate().atTime(offsetTime).withOffsetSameInstant(ZoneOffset.UTC);` a bit simpler. – Ole V.V. May 14 '19 at 08:54
  • @OleV.V. Thanks, that is much better. – Andreas May 14 '19 at 15:24
  • Thank you Andreas, Ole, Your solution worked for me. – Sahil May 15 '19 at 17:04