Serializing and Deserializing a POJO with LocalDate into JSON using JSON-B and Java EE 8

To serialize a POJO with JSON-B from Java EE 8 containing a java.time.LocalDate, you can either use the default format, or specify the serialization format with @JsonbDateFormat annotation:

import java.time.LocalDate;
import javax.json.bind.annotation.JsonbCreator;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.json.bind.annotation.JsonbProperty;

public class Developer {

    public LocalDateTime birthdate;

    @JsonbDateFormat(value = "yyyy-MM-dd")
    public LocalDate firstHack;

    public Developer(
            @JsonbProperty("birthdate") LocalDateTime birthdate, 
            @JsonbProperty("firstHack") LocalDate firstHack) {
        this.birthdate = birthdate;
        this.firstHack = firstHack;

Now the Developer can be serialized and deserialized with a javax.json.bind.Jsonb instance:

import java.time.LocalDate;
import java.time.Month;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;

public class DateSerializationTest {
    private Jsonb jsonb;
    public void init() {
        this.jsonb = JsonbBuilder.

    public void serialize() {
        Developer developer = new Developer(LocalDateTime.of(1995, Month.MARCH, 12, 12, 42), 
                LocalDate.of(1998, Month.MARCH, 12));
        String serialized = this.jsonb.toJson(developer);
        System.out.println("serialized = " + serialized);
        assertThat(serialized, containsString("birthdate"));


    public void deserialize() {
        String deserialzed = " {\"birthdate\":\"1995-03-12T12:42:00\",\"firstHack\":\"1998-03-12\"}";
        Developer duke = this.jsonb.fromJson(deserialzed, Developer.class);
        assertThat(duke.birthdate.getYear(), is(1995));


To run the example as JUnit test, you will need the following dependencies:


See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home:


Hi Adam

As long as all Clients and Backend Services are in same timezone, LocalDateTime will be fine.

If Javascript Client is sitting in different Timezone, It might be better to not to use locale Date but
UTC or Date String including timezone on Server Side.

On Browser side when one is calling, he will gate timestamps to UTC:

The method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

Cheers Radek

Posted by Radek on December 09, 2017 at 10:27 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments