0

I have followed this SO answer for datetime conversion of 8601.

I will cite an example straight from w3 :

1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.

1994-11-05T13:15:30Z corresponds to the same instant.

And this is what I run in android

SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
dateTime = sdfSource.parse("2014-03-06T11:30:00-05:00");
System.out.println(dateTime); //Thu Mar 06 18:30:00 EET 2014

Obviously .parse()'s output is the local aware datetime. There has been a conversion from EST(-05:00) to EET (+02:00) since now I am in this timezone. However I do not want this auto-convertion.

Is there a way to parse a datetime string inyyyy-MM-dd'T'HH:mm:ssZZZZZ format and display THAT timezone's datetime? Preferable output:

Thu Mar 06 11:30:00 EST 2014

The EST and my location is an example. It can be any other timezones as well.

Community
  • 1
  • 1
Diolor
  • 13,181
  • 30
  • 111
  • 179

3 Answers3

2

Internally Date objects are in UTC and that's what they're parsed to.

You cannot retrieve the original timezone from the Date but you can attempt to retrieve it from the original ISO-8601 stamp, and use it when formatting.

When you convert it to a string with toString(), it uses your local settings to format the date. If you want a specific representation, use a formatter to format the output, e.g.

int rawTimeZoneOffsetMillis = ...; // retrieve from ISO-8601 stamp and convert to milliseconds
TimeZone tz = new SimpleTimeZone(rawTimeZoneOffsetMillis, "name");

DateFormat outputFormat = DateFormat.getDateTimeInstance();
outputFormat.setTimeZone(tz);
System.out.println(df.format(dateTime));

ISO-8601 timestamps are not completely parseable with SimpleDateFormat. This answer has some code to work around some of the limitations.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
  • The limitations your are talking about is the `Z` aren't you? Also two things: I use `int rawTimeZoneOffsetMillis = dateTime.getTimezoneOffset()*1000` as the simplest way but I get a deprecation warning. And more importantly `TimeZone tz = new TimeZone();` gives an error: `Cannot instantiate the type TimeZone`. – Diolor Mar 06 '14 at 11:03
  • Additionally, Java `Date` cannot represent microseconds resolution like ISO-8601 stamps can. Overlooked the docs, `TimeZone` is abstract and you should use `SimpleTimeZone` instead. The deprecated `Date.getTimezoneOffset()` returns the offset of the *current* timezone, not that that was there in the original stamp. – laalto Mar 06 '14 at 11:12
0

Although you should not be worried while parsing the date as it is parsed to correct value can be displayed in any format or timezone you want.

SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
sdfSource.setTimeZone( TimeZone.getTimeZone( "EST" ) );
dateTime = sdfSource.parse("2014-03-06T11:30:00-05:00");
System.out.println(sdfSource.format(dateTime)); //Thu Mar 06 18:30:00 EET 2014
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • Thanks but I want a `Thu Mar 06 11:30:00 EST 2014` output. – Diolor Mar 06 '14 at 10:27
  • Yes still prints `Thu Mar 06 18:30:00 EET 2014` – Diolor Mar 06 '14 at 11:53
  • i will. Check my edited ans it will work. Laalto has explained how it works. The thing is date only has one long value that can be converted in any format you want, for that you need to call format function to get formated string – vipul mittal Mar 06 '14 at 11:56
0

Use sdfSource.setTimeZone() method

SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
sdfSource.setTimeZone(TimeZone.getTimeZone("EST")); //give the timezone you want
dateTime = sdfSource.parse("2014-03-06T11:30:00-05:00");
System.out.println(dateTime); //Thu Mar 06 18:30:00 EET 2014

This should do fine.

jibs
  • 120
  • 1
  • 4
  • 15