0

Hi I have a date object in my controller request mapping. The object is of ZoneDateTime. The problem is on parsing the data to ZoneDateTime it is converting it to UTC by default. I need to retain the timezone information. Is there a way to handle that.

 ex: 2018-06-10T12:00:00+0500

value in my controller:

 2018-06-10T07:00:00[UTC]

I am planning to use an object mapper to fix it while marshaling of data in controller. But i am not sure if i am heading to right direction.

Thanks.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Madie
  • 231
  • 3
  • 10
  • 1
    Please do not use the [java-ee] tag when you have a question about a class in the Java SE API, simply because that class is clearly not part of Java EE API. – BalusC Aug 23 '18 at 08:52
  • Without code I’ve got no idea, sorry. Please [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Ole V.V. Aug 23 '18 at 09:35
  • Search Stack Overflow thoroughly before posting. – Basil Bourque Aug 23 '18 at 16:09

1 Answers1

2

The string you're parsing doesn't have time zone information. The appropriate type to use is OffsetDateTime:

OffsetDateTime dt = OffsetDateTime.parse("2018-06-10T12:00:00+0500", 
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX"))

You can convert that to a ZonedDateTime by calling dt.toZonedDateTime().

ernest_k
  • 44,416
  • 5
  • 53
  • 99