Skip to content Skip to sidebar Skip to footer

Datetime Parsing Error

I am having problem parsing dates in Java. Below is the code. String dateString = '2017-12-13T16:49:20.730555904Z'; List formatStrings = Arrays.asList('yyyy-MM-dd'T

Solution 1:

S in SimpleDateFormat specifies a number of milliseconds, not a fraction of a second. You've specified 730555904 milliseconds, which is ~8.45 days - hence the date change.

java.util.Date only has a precision of milliseconds. I would recommend using the java.time package, which has a precision of nanoseconds, like your input. Use DateTimeFormatter for parsing. Note that in DateTimeFormatter, the S specifier is for fraction-of-a-second.

Even better, Instant.parse uses the right format anyway:

import java.time.*;

publicclassTest {
    publicstaticvoidmain(String... args)throws Exception {
        Stringtext="2017-12-13T16:49:20.730555904Z";
        Instantinstant= Instant.parse(text);
        System.out.println(instant);
    }
}

Post a Comment for "Datetime Parsing Error"