We Are Going To Discuss About Deserialize Java 8 LocalDateTime with JacksonMapper. So lets Start this Java Article.
Deserialize Java 8 LocalDateTime with JacksonMapper
- Deserialize Java 8 LocalDateTime with JacksonMapper
The date time you're passing is not an ISO local date time format.
Change to@Column(name = "start_date") @DateTimeFormat(iso = DateTimeFormatter.ISO_LOCAL_DATE_TIME) @JsonFormat(pattern = "YYYY-MM-dd HH:mm") private LocalDateTime startDate;
- Deserialize Java 8 LocalDateTime with JacksonMapper
The date time you're passing is not an ISO local date time format.
Change to@Column(name = "start_date") @DateTimeFormat(iso = DateTimeFormatter.ISO_LOCAL_DATE_TIME) @JsonFormat(pattern = "YYYY-MM-dd HH:mm") private LocalDateTime startDate;
Solution 1
The date time you’re passing is not an ISO local date time format.
Change to
@Column(name = "start_date")
@DateTimeFormat(iso = DateTimeFormatter.ISO_LOCAL_DATE_TIME)
@JsonFormat(pattern = "YYYY-MM-dd HH:mm")
private LocalDateTime startDate;
and pass the date string in the format ‘2011-12-03T10:15:30’.
But if you still want to pass your custom format, you just have to specify the right formatter.
Change to
@Column(name = "start_date")
@DateTimeFormat(iso = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
@JsonFormat(pattern = "YYYY-MM-dd HH:mm")
private LocalDateTime startDate;
I think your problem is the @DateTimeFormat has no effect at all. As the Jackson is doing the deserialization and it doesn’t know anything about spring annotation, and I don’t see spring scanning this annotation in the deserialization context.
Alternatively, you can try setting the formatter while registering the Java time module.
LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
Here is the test case with the deseralizer which works fine. Maybe try to get rid of that DateTimeFormat annotation altogether.
@RunWith(JUnit4.class)
public class JacksonLocalDateTimeTest {
private ObjectMapper objectMapper;
@Before
public void init() {
JavaTimeModule module = new JavaTimeModule();
LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);
objectMapper = Jackson2ObjectMapperBuilder.json()
.modules(module)
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
}
@Test
public void test() throws IOException {
final String json = "{ \"date\": \"2016-11-08 12:00\" }";
final JsonType instance = objectMapper.readValue(json, JsonType.class);
assertEquals(LocalDateTime.parse("2016-11-08 12:00",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm") ), instance.getDate());
}
}
class JsonType {
private LocalDateTime date;
public LocalDateTime getDate() {
return date;
}
public void setDate(LocalDateTime date) {
this.date = date;
}
}
Original Author s7vr Of This Content
Solution 2
This worked for me:
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ", shape = JsonFormat.Shape.STRING)
private LocalDateTime startDate;
Original Author user5515 Of This Content
Solution 3
You used wrong letter case for year in line:
@JsonFormat(pattern = "YYYY-MM-dd HH:mm")
Should be:
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
With this change everything is working as expected.
Original Author Maciej Walkowiak Of This Content
Solution 4
There are two problems with your code:
1. Use of wrong type
LocalDateTime
does not support timezone. Given below is an overview of java.time types and you can see that the type which matches with your date-time string, 2016-12-01T23:00:00+00:00
is OffsetDateTime
because it has a zone offset of +00:00
.
Change your declaration as follows:
private OffsetDateTime startDate;
2. Use of wrong format
There are two problems with the format:
- You need to use
y
(year-of-era ) instead ofY
(week-based-year). Check this discussion to learn more about it. In fact, I recommend you useu
(year) instead ofy
(year-of-era ). Check this answer for more details on it. - You need to use
XXX
orZZZZZ
for the offset part i.e. your format should beuuuu-MM-dd'T'HH:m:ssXXX
.
Check the documentation page of DateTimeFormatter
for more details about these symbols/formats.
Demo:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String strDateTime = "2019-10-21T13:00:00+02:00";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:m:ssXXX");
OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtf);
System.out.println(odt);
}
}
Output:
2019-10-21T13:00+02:00
Learn more about the modern date-time API from Trail: Date Time.
Original Author Arvind Kumar Avinash Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.