Home » Java » Parse String to date with timezone offset intact

Parse String to date with timezone offset intact

Posted by: admin June 30, 2018 Leave a comment

Questions:

We have a client sending date to us in String format as "2017-06-14T04:00:00-08:00". We need to convert it to JAVA Date type before working with it.

We are parsing in this way:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); 
dateParsedFromString = formatter.parse("2017-06-14T04:00:00-08:00");

But we are losing the offset after this parse. When we convert it back to string we are seeing value:

2017-06-14T08:00:00-04:00

How can I convert from String to Date in JAVA without changing the offset?

Answers: