One day shift between bootstrap datepicker and LocalDate

761 views Asked by At

I have a form where I use a field LocalDate that receives the value from a form mapped through Jackson. While JSON is:

Tue Jun 27 2017 00:00:00 GMT+0200 (ora legale Europa occidentale)   

My LocalDate variable is 2017-06-26 This is my pojo:

public class BookingForm {
    private Integer building;
    private Integer city;
    private LocalDate date;
    private String description;
    private LocalTime endTime;
    private List<Integer> externalUsers;
    private List<Integer> internalUsers;
    private String name;
    private Room room;
    private LocalTime startTime;
    private List<ExternalUser> newExternalUsers;
    //get and set method

And I have introduced Jackson for JDK8:

<!-- Jackson dependencies -->
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-hibernate5</artifactId>
    <version>${jacksondatatype.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jdk8</artifactId>
    <version>${jacksondatatype.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${jacksonjsr310.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-parameter-names</artifactId>
    <version>${jacksondatatype.version}</version>
</dependency>

With Date instead of LocalDate all work fine.
Is there a problem with timezone or what else?

UPDATE: This is my Spring configuration:

public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    //authomatic registration
    mapper.findAndRegisterModules();

    messageConverter.setObjectMapper(mapper);
    return messageConverter;

}

is it possible to configure here?

2

There are 2 answers

1
amittn On

I think your Jackson is set to use GMT and your BookingForm may be using something other like BST which is causing this

2
AudioBubble On

You need to add the JavaTimeModule with a deserializer set, and this deserializer must have a DateTimeFormatter (to parse the input).

For this test, I created a sample class:

public class JsonLocalDate {
    private LocalDate date;

    // getter/setter
}

And then:

String json = "{ \"date\": \"Tue Jun 27 2017 00:00:00 GMT+0200\" }";

ObjectMapper mapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
// create formatter for the pattern
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
    // append pattern
    .appendPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z")
    // create formatter - use English Locale to parse weekday and month name
    .toFormatter(Locale.ENGLISH);
// add the LocalDateDeserializer with the custom formatter
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(formatter));
mapper.registerModule(module);

JsonLocalDate value = mapper.readValue(json, JsonLocalDate.class);
System.out.println(value.getDate());

The output is:

2017-06-27