Summary
I am attempting to parse dates such as 25/Sep/17
hence the SimpleDateFormat annotation seems to be @JsonFormat(pattern = "dd/MMM/yy")
. However, when I try to parse this, I get an InvalidFormatException where the essence is (full exception below after example):
Text '25/Sep/17' could not be parsed at index 3
What is wrong with my date format string? I can't see any issues. It looks like it doesn't like the Sep
paired with MMM
.
I am using Amazon Corretto Java version "11.0.5" 2019-10-15 LTS
Example to reproduce
package example;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.LocalDate;
public class HelloMapperApp {
private static final ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) {
MAPPER.registerModule(new JavaTimeModule());
try {
Example example = MAPPER.readValue("{ \"name\": \"example\", \"date\": \"25/Sep/17\"", Example.class);
System.out.println("Deserialised is: " + example);
} catch (Exception e) {
throw new RuntimeException("Could not parse JSON: " + e);
}
}
static class Example {
@JsonProperty
String name;
@JsonProperty
@JsonFormat(pattern = "dd/MMM/yy")
LocalDate date;
@Override
public String toString() {
return String.format("name: %s%ndate: %s", name, date);
}
}
}
Full Exception
Exception in thread "main" java.lang.RuntimeException: Could not parse JSON: com.fasterxml.jackson.databind.exc.InvalidFormatException:
Cannot deserialize value of type
`java.time.LocalDate` from String "25/Sep/17": Failed to deserialize java.time.LocalDate:
(java.time.format.DateTimeParseException) Text '25/Sep/17' could not be parsed at index 3
at [Source: (String)"{ "name": "example", "date": "25/Sep/17""; line: 1, column: 30] (through reference chain: example.HelloMapperApp$Example["date"])
at example.HelloMapperApp.main(HelloMapperApp.java:19)