convert iso to utc format in java

Viewed 113

I'm getting below exception while converting date string from ISO to UTC format in java, what I'm doing wrong here? Please find my code below:

Test.java

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        String date="2020-11-02T07:00:00.114";
        String result = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")).atOffset(ZoneOffset.UTC).toString();
        System.out.println("result: "+result);
    }
}

Exception:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-11-02T07:00:00.114' could not be parsed, unparsed text found at index 19
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at test.service.Test.main(Test.java:11)
3 Answers

tl;dr

LocalDateTime
.parse( "2020-11-02T07:00:00.114" ) 
.atOffset( ZoneOffset.UTC )
.toString()

2020-11-02T07:00:00.114Z

Details

You said:

converting date string from ISO to UTC format

That does not exactly make sense.

  • There is an ISO 8601 standard defining sensible formats for exchanging date-time values textually. These standard formats are used by default when parsing/generating text.
  • There is no such thing as "UTC format". UTC is the temporal prime meridian, against which all time zones are defined. The rules for a time zone specify an offset-from-UTC in effect as a number of hours-minutes-seconds ahead or behind UTC. But this has nothing to with "formats" of text.

As for parsing your input string as a LocalDateTime object, there is no need to bother with defining a formatting pattern. Your string complies with ISO 8601, so it can be parsed directly.

LocalDateTime ldt = LocalDateTime.parse( "2020-11-02T07:00:00.114" ) ;

Your string, and a LocalDateTime object, represent a date with a time-of-day. But both lack the context of a time zone or offset-from-UTC.

Apparently you know that the publisher of your input string intended that to be a moment as seen from an offset of zero hours-minutes-seconds from UTC. So we can assign a ZoneOffset to get OffsetDateTime object.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

To generate text representing that moment in ISO 8601 format, simply call toString.

String output = odt.toString() ;

See this code run live at Ideone.com.

2020-11-02T07:00:00.114Z

Notice the Z on the end, pronounced “Zulu”. That is an abbreviation for an offset of zero, +00:00.

Your pattern ends in ss, in other words: In seconds. Your input then has still more stuff.

DateTimeFormatter doesn't take a wild stab at it - it either matches properly or it does not parse. It looks like those are intended to be milliseconds; toss .SSS at the end of your pattern.

You are giving the format of:

yyyy-MM-dd'T'HH:mm:ss

However are specifying a string with fractions of a second 2020-11-02T07:00:00.114.

That is parsing whole seconds, but there is still more in the time string.

You need format of:

yyyy-MM-dd'T'HH:mm:ss.SSS

Related