0
votes

I believe this should work. Granted it has no specified offset, but shouldn't that then default to UTC? And if not, how can I parse a string like this into an OffsetDateTime?

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'[HH:mm:ss][.SSSSS]][z][x]");
String datetime = "2018-12-03T18:07:55";
OffsetDateTime odt = OffsetDateTime.parse(datetime, formatter);

Exception thrown: java.time.format.DateTimeParseException: Text '2018-12-03T18:07:55' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2018-12-03T18:07:55 of type java.time.format.Parsed

1

1 Answers

0
votes

As you say, it's failing because there's no offset in the format. To work around this, you can use the same format to parse to a LocalDateTime, and then combine with a ZoneOffset to create an OffsetDateTime:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd['T'[HH:mm:ss][.SSSSS]][z][x]");
String datetime = "2018-12-03T18:07:55";

LocalDateTime ldt = LocalDateTime.parse(datetime, formatter);
OffsetDateTime odt = OffsetDateTime.of(ldt, ZoneOffset.UTC);

System.out.println(odt);