Handling Java 8 Dates and Time with Jackson (JSR-310)

04.05.2017 by Jens in Spring Boot | Spring MVC

When you are starting a new Spring Boot application today, you will probably want to use Java 8. However, it still can be tricky with certain frameworks when using new features like the Date and Time API (known as JSR-310).

Jackson is one of them. By Default, it does not handle the new Java 8 date and time classes correctly as dates but rather serializes them as objects.

But, let’s take a closer look at the problem and apply an easy fix.


So, for example, if we return a Instant like in the following example

public class DateProblemExampleController {

    public Instant getNow() {
        return Instant.now();

We would get this as the JSON response.


When we use a LocalDateTime, it will be even more complex.

    "day_of_year": 124,
    "month_value": 5,
    "year": 2017,
    "month": "MAY",
    "day_of_month": 4,
    "day_of_week": "THURSDAY",
    "hour": 10,
    "minute": 49,
    "second": 7,
    "nano": 10000000,
    "chronology": {
        "id": "ISO",
        "calendar_type": "iso8601"


Luckily, the solution is pretty simple and straight forward. The Jackson framework provides an extension for correctly handling Java 8 dates and times. Just add it to your pom


The correct version is set in the dependency Managment of the Spring Boot parent. If you are using it outside of Spring Boot, you need to add the version number.

If you are using the default ObjectMapper the handling of Java 8 date is picked up automatically by Spring Boot. However, if you have defined your own ObjectMapper you might need to register the module.

registerModule(new JavaTimeModule());

Now, we can call our API again.

[2017, 5, 4, 11, 20, 49, 44000000]

Looks better, but still not useable. So, let’s adjust the ObjectMapper to handle dates not as timestamps using the SerializationFeature.WRITE_DATES_AS_TIMESTAMPS property.

public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    ObjectMapper objectMapper = builder.build();
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return objectMapper;

When you use Spring Boot, you could also set this adding the following line to the application.properties file:


Spring Boot enables the configuration of the default ObjectMapper with properties under the spring.jackson prefix. Read the jackson section in the Spring Boot docs for more info on this.

Now we receive the values in a more useable format.

For Instant


For LocalDateTime



Still a nuisance with the support of Java 8 features by default. However, in the case of Jackson, it is straight forward to fix it and make it useable in Spring Boot.

Want to improve as a dev?

Stay up-to-date with my Learnletter and I bet you will learn a new thing or two.

I send a short email every workday about Spring topics, API development and improving as a dev. The current series is about building a Kanban API with Spring.

Comes with a FREE Spring Boot Cheatsheet.

comments powered by Disqus