0

I would need the community's help because I could not find the answer in the Java documentation. I don't understand how the offset is taken into the math calculations when I try to convert an OffsetDateTime (ex: 2022-07-09T11:30:34) object to an Instant object. For example:

If we would run on OpenJDK 1.8 the command in a main function: OffsetDateTime.parse("2022-07-09T12:30:34+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME).toInstant() the outcome would be an Instance of date-and-time 2022-07-09T11:30:34 when I would had expected an Instant of 2022-07-09T13:30:34. The difference is the hour. Why do I get it like this?

And the opposite using -01:00 will do the revet.

I apologize for not formatting my text correctly or if I missed something. I would appreciate it if my post would not be marked us not worthy. And sorry if the answer was already answered in a different thread, which I could not find.

Thank you in advance.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • FYI, `OffsetDateTime.parse("2022-07-09T12:30:34+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME)` is redundant, as that formatter is used by default in `OffsetDateTime.parse("2022-07-09T12:30:34+01:00")`. – Basil Bourque Jul 11 '22 at 20:18
  • "when I would had expected an Instant of 2022-07-09T13:30:34" — The `+01:00` in "2022-07-09T12:30:34+01:00" means an hour *ahead* of UTC, per the ISO 8601 standard used in *java.time*. So moving from there to UTC means moving the clock *backwards*, from 12:xx hour to 11:xx hour. Your expectation of moving forward an hour is incorrect in *java.time*, being inconsistent with ISO 8601. Be aware that some date-time protocols parse the offset in a manner opposite that of ISO 8601, and in those protocols your expectation would be correct. Isn't date-time work fun? – Basil Bourque Jul 11 '22 at 20:20
  • Great answer. Thank you. Yes, it is a bit weird because I need to constantly check the date-time parse protocol, but now, thank to you all, I do have the knowledge to know how to see things. – Greşanu Emanuel - Vasile Jul 12 '22 at 12:04

0 Answers0