2

Possible Duplicate:
Parse DateTime with timezone of form PST/CEST/UTC/etc

OK, I have following code in Java that converts my Date object into String:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
String str = dateFormat.format(new Date());
// outputs: 2012-12-17 15:44:57 CST

I am sending that String over the wire to WebService written in C#. So, how would I parse back that String into valid DateTime considering that following doesn't work because zzz in C# is different:

DateTime.ParseExact(parts[2], "yyyy-MM-dd HH:mm:ss zzz", CultureInfo.InvariantCulture.DateTimeFormat);

And before anyone suggests it - I know I can go with UTC time, but I need to do it this way.

Any ideas?

Community
  • 1
  • 1
nikib3ro
  • 20,366
  • 24
  • 120
  • 181
  • 1
    Possible duplicates [here](http://stackoverflow.com/questions/241789/parse-datetime-with-timezone-of-form-pst-cest-utc-etc), [here](http://stackoverflow.com/questions/8754563/parse-string-datetime-containing-timezone-info), and [here](http://stackoverflow.com/questions/193095/why-cant-net-parse-a-date-string-with-a-timezone) – D Stanley Dec 17 '12 at 21:55
  • Why do you _need to do it this way_? When calling the web service why not convert the date to whatever format you want? – Dave Zych Dec 17 '12 at 22:02

1 Answers1

2

CST is not a valid .NET timezone identifier.

In .NET, these are all numeric offsets - +0700, -0500 etc...

You will need to output a numeric timezone offset if you want it to be parsed by .NET.

I suggest using yyyy-MM-dd HH:mm:ss Z in Java, as can be seen here.

The alternative is to convert CST to -0600 using string.Replace, but this is both not scalable and can fall over with the ambiguities of named timezones (CST can mean different timezones depending on where you are).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • You obviously haven't read this part: "So, how would I parse back that String into valid DateTime ___considering that following doesn't work because zzz in C# is different___" – nikib3ro Dec 17 '12 at 21:56
  • @kape123 - You either change the Java to use `Z` instead of `zzz` or, in C# convert the named timezones to their numeric equivalents (though the named timezones are ambiguous) – Oded Dec 17 '12 at 21:58
  • Didn't know about Z - thanks! So Z on Java side, zzz on C# side and it works perfectly. – nikib3ro Dec 17 '12 at 22:10