2

I am sending date from front end as below format as string.

[{"StartDate":"2018-03-09"}]

In spring i am using ObjectMapper to get this in POJO. While using ObjectMapper.readValue is converting to below date.

Fri Mar 09 05:30:00 IST 2018

I already tried below code.

ObjectMapper objectMapper = new ObjectMapper().setTimeZone(TimeZone.getTimeZone("UTC"));

But its not working for me. Still getting Fri Mar 09 05:30:00 IST 2018 as output.

How to make code independent of timezone.

1 Answers1

0

You are mapping string date to Date object. You can't have date object without timezone. The UTC timezone is being applied correctly by mapper and date is getting stored as 2018-03-09 00:00:00 UTC and for IST that becomes 2018-03-09 05:30:00 IST. If you want 2018-03-09 00:00:00 IST time, instead of UTC, pass IST.

If you are looking to send out date without timezone, you can do that with annotation as shown below (requires json-jackson version >= 2.0)

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone="Asia/Kolkata")
private Date date;
Yogesh Badke
  • 4,249
  • 2
  • 15
  • 23
  • I am getting below error. Failed to parse Date value '2018-03-09' (format: "yyyy-MM-dd HH:mm a z") – laimil patel Mar 09 '18 at 10:26
  • then it give output for 8th march. i tried to send date as 2018-03-08T18:30:00.000Z also. please give correct format for this. – laimil patel Mar 09 '18 at 10:31
  • Edited the answer with option to provide timezone. Can you check now? Also, make sure you are not using 2 different object mappers with diff configurations. (Possibly one of your own and one created by spring) – Yogesh Badke Mar 09 '18 at 10:36
  • sorry cant upload code. Using below code for object mapping objectMapper.readValue(inputData, TypeFactory.defaultInstance().constructCollectionType(List.class, DTO.class)); I am simply sending date from front end. Trying to map date with my POJO variable. – laimil patel Mar 12 '18 at 14:06