Conversion
I have a timestamp with an offset (-6) in the following format:
2019-11-30T00:01:00.000-06:00
and I want to convert it to an UTC timestamp, like:
2019-11-30T06:01:00.000Z
Attempt
I tried it the following way:
String text = "2019-11-30T00:01:00.000-06:00";
LocalDate date = LocalDate.parse(text, DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(date.toInstant());
but it is not compiling:
The method
toInstant()is undefined for the typeLocalDate
How am I supposed to do this correctly?
tl;dr
Explanation
Your date is not just a date, it also has time. So
LocalDatewould not work. If at all, thenLocalDateTime.But, it is also not a local date/time, it has offset information. You need to use
OffsetDateTimeinstead, and then go toInstantfrom there.To actually get the desired output for your
Instant, you also have to create a properDateTimeFormatter, since the default representation does not include the millis.