Formatting a Java 8 LocalDateTime in JSON with Spring Boot

Java 8 DateTime API

A common issue with Java 8 and Spring Boot was that formatting Java 8 dates in JSON wasn’t the most straightforward. This includes classes such as LocalDate, LocalDateTime and LocalTime. With the newer versions of Spring Boot however, this has been fixed and it seems that this is still unknown to a lot of developers. In this blogpost, I’m going to show a small example application which should hopefully help in formatting Java 8 dates in a Spring Boot app.

To start, let’s head to start.spring.io and create a starter project. I choose a project based on Gradle, Java and Spring Boot 2.1.2, but if you prefer Maven, that will work too. Enter a group and artifact name, and at the dependencies, choose ‘web’. Click on the generate button, and your project should download.

How to implement Java 8 date time formatting?

To continue, open the downloaded project in your favorite IDE, and add the following code:

@RestController
public class DateTimeController {

    private Clock clock = Clock.fixed(Instant.parse("2019-02-05T16:45:42.01Z"), ZoneId.of("Australia/Sydney"));

    @GetMapping("/time")
    public DateTimeDto timeMapping() {
        return new DateTimeDto(clock);
    }
}

public class DateTimeDto {

    private LocalDate localDate;
    private LocalDateTime localDateTime;
    private LocalTime localTime;

    public DateTimeDto(Clock clock) {
        localDate = LocalDate.now(clock);
        localDateTime = LocalDateTime.now(clock);
        localTime = LocalTime.now(clock);
    }

    public LocalDate getLocalDate() {
        return localDate;
    }

    public LocalDateTime getLocalDateTime() {
        return localDateTime;
    }

    public LocalTime getLocalTime() {
        return localTime;
    }
}

See for the full source the GitHub project. The following code, when accessing http://localhost:8080/time, will now produce output similar to the following output:

{
  "localDate": "2019-02-05",
  "localDateTime": "2019-02-05T22:08:28.097832",
  "localTime": "22:08:28.097869"
}

How to test Java 8 date time formatting?

To test the above in a more structured way, you can use MockMvc to write a unit test for this code:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
class DateTimeControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void timeMapping() throws Exception {
        this.mockMvc.perform(get("/time"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().json("{'localDate':'2019-02-06'}"))
                .andExpect(content().json("{'localDateTime':'2019-02-06T03:45:42.01'}"))
                .andExpect(content().json("{'localTime':'03:45:42.01'}"));
    }
}

To get more control over the formatting of the date and time, you can use the @JsonFormat annotation, like this:

@Override
@JsonFormat(pattern="dd-MM-yyyy")
public LocalDate getLocalDate() {
    return super.getLocalDate();
}

I hope the above helps in formatting Java 8 dates and times in a Spring Boot 2 app, and please checkout the GitHub project for a full example.

Leave a Reply

Your email address will not be published. Required fields are marked *