0

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?

Vamsi K.N
  • 39
  • 1
  • 5
  • Possible duplicate of [How to parse dates with -0400 timezone string in python?](http://stackoverflow.com/questions/1101508/how-to-parse-dates-with-0400-timezone-string-in-python) – Mario Santini May 17 '17 at 07:36
  • 4
    @MarioSantini How can a answer for phyton be a dupplicate for a question in Java? – Jens May 17 '17 at 07:36
  • @Jens you were right, sorry I just miss up the links, have a look here http://stackoverflow.com/questions/4203718/converting-string-to-date-with-timezone is a java anser, could not be flagged as it is not accepted answer. – Mario Santini May 17 '17 at 07:42
  • @Jens No, when I printed it without changing back, I got the incorrect time as "Wed Jun 14 08:00:00 EDT 2017". I gave the output after changing it to string as it better explains my requirement. – Vamsi K.N May 17 '17 at 08:55

1 Answers1

2

java.util.Date doesn't store time zone information.

To retain time zone, use ZonedDateTime or OffsetDateTime (Java 8+).

Since your date string is ISO 8601, you won't even need to specify a date format.

ZonedDateTime zdt = ZonedDateTime.parse("2017-06-14T04:00:00-08:00");
System.out.println(zdt); // prints: 2017-06-14T04:00-08:00
OffsetDateTime odt = OffsetDateTime.parse("2017-06-14T04:00:00-08:00");
System.out.println(odt); // prints: 2017-06-14T04:00-08:00

For pre-Java 8, use the ThreeTen-Backport:

ThreeTen-Backport provides a backport of the Java SE 8 date-time classes to Java SE 6 and 7.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    While `ZonedDateTime` technically works, `OffsetDateTime` is appropriate. A time zone is a history of offset changes used by a particular region. – Basil Bourque May 17 '17 at 07:59
  • @BasilBourque Using `ZonedDateTime` is perfectly valid, since `-08:00` is a valid time zone. Time zone is a superset of zone offset, or rather, [`ZoneOffset`](https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html) is a subclass (restriction) of `ZoneId`. – Andreas May 17 '17 at 08:02
  • Then why do we have the `OffsetDateTime` class? And, no, `-08:00` is *not* a time zone. `America/Los_Angeles` is a time zone. That zone has an offset of `-08:00` part of the year and `-07:00` in another part of the year. Those changes in offset over time, their history in the past, the present offset at this moment, and the planned offset changes scheduled in the future, that *collection of offsets* for that one region is a time zone. So your use of `ZonedDateTime` in this Answer is misleading, confusing, and not appropriate to the input data. Otherwise good Answer if you delete ZonedDateTime. – Basil Bourque May 17 '17 at 08:10
  • @Andreas Thanks for the solution, but unfortunately we are not using JAVA 8 – Vamsi K.N May 17 '17 at 08:52
  • @BasilBourque An offset-only ZoneId is a perfectly valid ZoneId, so using `ZonedDateTime` is perfectly valid too. Sure, you can *restrict* it to offset-only, by using `OffsetDateTime`, and if you know that input will only ever be that, it may be a good idea, but use of `ZonedDateTime` is not an invalid or misleading answer, and the input is perfectly appropriate for a `ZonedDateTime`. It is still a good/useful answer, even with `ZonedDateTime` in it. – Andreas May 17 '17 at 09:25