0

I have the following API

public ResponseEntity<ResponseDataToSeller> getTrackingDataByDate(@RequestBody RequestBodyBySeller requestBodyBySeller) {
    System.out.println(requestBodyBySeller.getEndDate());
    System.out.println(requestBodyBySeller.getEndDate().getDate());
    System.out.println(requestBodyBySeller.getEndDate().getHours());

public class RequestBodyBySeller {
    @ApiModelProperty(dataType="Date", notes = "endDate",required = true)
    Date endDate;

When I make a request with "endDate": "2018-10-11", it prints

2018-10-11 16:54:45.897  INFO 11996 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 3 ms
Wed Oct 10 20:00:00 EDT 2018
10
20

This is messing up SQL queries because I'm testing if hit_date <= #{endDate}. It should be returning results from 2018-10-11 but it's not, because Spring Boot has interpreted the date as 2018-10-10. How can I get Spring Boot to use UTC time zone when parsing the date parameters to an API?

Spring Boot 1.5.13, MyBatis 3.4.5, MyBatis Spring 1.3.1

3 Answers 3

0

Use local date, the problem is that when dates are involved, java tries to convert the date to the server timezone, since the is no time zone specified, it takes the hour ad UTC and then converts, LocalDate does not contain the timezone info, so, the date will be parsed as is.

Sorry, forgot to mention, to handle local date time you need to use jackson-datatype-jsr310. Then, as the example says, you can annotate your class with:

@JsonFormat(pattern = "dd/MM/yyyy")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate birthdate;*
2
  • That didn't work. Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value ('2018-10-11'); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value ('2018-10-11') at [Source: java.io.PushbackInputStream@56bc399c; line: 1, column: 35]
    – Chloe
    Oct 11, 2018 at 21:33
  • That won't compile. Eclipse gives the error Type mismatch: cannot convert from Class<JodaDeserializers.LocalDateDeserializer> to Class<? extends JsonDeserializer>.
    – Chloe
    Oct 12, 2018 at 17:39
0

There is some way to define jackson feature in springmvc when you use springboot.

  1. Use properties. spring.jackson.xxxx. Such as you want to set timezone. use add spring.jackson.time-zone=xxx in your application.properties

  2. define a spring bean which implement Jackson2ObjectMapperBuilderCustomizer

  3. you can directly create ObjectMapper bean in spring ,custom you want.

5
  • #1 didn't actually work. It still prints Wed Oct 10 20:00:00 EDT 2018. Even with a full restart. spring.jackson.time-zone=UTC
    – Chloe
    Oct 12, 2018 at 17:52
  • Your code print ‘’date“ show that it is EDT timezone。So you can set spring.jackson.time-zone=EDT to try。
    – Nick.Guo
    Oct 16, 2018 at 6:30
  • I think i need say something more.You print the "date" show EDT timezone. This "EDT" was come from the jvm or your system.I think the cause is EDT is later UTC 4H.So the UTC time ”2018-10-11“ was changed to EDT time "Wed Oct 10 20:00:00 EDT 2018".So you need keep the timezone the same ,spring.jackson.time-zone and the jvm (or system) timezone.
    – Nick.Guo
    Oct 16, 2018 at 6:59
  • No, my development server is in EDT and the timestamps are in the database and are UTC and given in the API as UTC. I don't know how Spring Boot finds my time zone as I don't specify, but it might get it from the JVM or Windows. I don't want to set the time zone to EDT. It already knows my time zone is EDT. I just want to interpret the date in UTC zone or set the Date with UTC zone, because the dates are in the DB as UTC and given on the API in UTC. I want to make my development server be like production, which will run in UTC zone.
    – Chloe
    Oct 16, 2018 at 17:51
  • In the case,you should change timezone with UTC in jvm
    – Nick.Guo
    Oct 17, 2018 at 2:13
0

I changed the class to java.time.LocalDate and added this dependency

    <!-- For LocalDate to be deserialized using ISO format -->
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>

https://stackoverflow.com/a/46884603/148844

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.