2

I need to parse LocalDateTime. I reseive this String, for example:

2020-06-05T13:54:24+03:00

Okay, I see date time with timeshift here. I added jackson datatype library:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>${jackson.version}</version>
    </dependency>

I set up ObjectMapper:

@Bean
public ObjectMapper objectMapper() {
    return new ObjectMapper()
            .registerModule(new JavaTimeModule())
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}

& I set up deserialization pattern:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
private LocalDateTime createDateTime;

or like this:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
private LocalDateTime createDateTime;

but I reseive this exception:

Caused by: java.time.format.DateTimeParseException: Text '2020-06-05T13:54:24+03:00' could not be parsed, unparsed text found at index 19

if I parse this string this way:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"); 
LocalDateTime dateTime = LocalDateTime.parse("2020-06-05T13:54:24+03:00", formatter);

all okay.

As I understand, problem is in timeshift. How to solve it?

  • The index at 19 is the `+` sign. the text after that is `3:00` which is not in any known format. Perhaps it is a mistake in the input? offset patterns are here. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html try with `yyyy-MM-dd'T'HH:mm:ssxxx` edit: SimpleDateFormat is different it seems. According to https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html it should be `XXX` so weird – Abhishek Dujari Jun 05 '20 at 11:35
  • 1
    Use only `X` instead of using `XXX`. Similar question here https://stackoverflow.com/questions/32694653/date-formats-difference-between-yyyy-mm-ddthhmmss-and-yyyy-mm-ddthhmmssx – Zedex7 Jun 05 '20 at 13:08

0 Answers0