My xmlGregorianCalendar value coming as 2020-10-02T13:07:38-06:00 .. I want to pass thisxmlGregorianCalendar value and get the output like 2020-10-02T07:07:38(which reduces 6 hours from the time) any suggestion on this please?
I have used this below method.
return new Timestamp(xmlGregorianCalendar.toGregorianCalendar(TimeZone.getTimeZone("GMT"),null,null).getTimeInMillis())..
But it removing the off set value but not adjusting the time. the output i am getting is 2020-10-02T13:07:38..But i am expecting the 2020-10-02T07:07:38 like this.
First of all, I recommend you switch from the outdated and error-prone
java.util
date-time API to the modernjava.time
date-time API. Learn more about the modern date-time API from Trail: Date Time.Your understanding of Zone-Offset is not correct
The date-time string,
2020-10-02T13:07:38-06:00
tells us that the given date and time has been adjusted with an offset of -06:00 hours fromUTC
i.e. the corresponding date-time atUTC
would be2020-10-02T19:07:38Z
whereZ
specifies a Zone-Offset of00:00
hours.It means that if you are expecting a date-time of
2020-10-02T07:07:38
, you need to offset the given date-time further by-06:00
hours i.e. it will be at a total of-12:00
hours offset fromUTC
.The following example illustrates this concept:
Output:
Using the legacy API:
Output: