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?

2
  • 1
    Possible duplicates here, here, and here Commented Dec 17, 2012 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? Commented Dec 17, 2012 at 22:02

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

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"
@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)
Didn't know about Z - thanks! So Z on Java side, zzz on C# side and it works perfectly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.