views:

122

answers:

2

Hi, clientside I have some scripts that send to the server some strings... one of which comes from a Javascript Date object. Now, it has it own formatting and I was just wandering if is there a class that does the right conversion, as I am experiencing problems with SimpleDateFormatter...

+1  A: 

Personally I prefer the Joda Time formatters for one main reason: they're thread-safe and immutable, so you can create one, keep it statically, and reuse it without any worries. Joda also allows easy specification of time zones etc. Of course, they end up creating Joda objects, which is another advantage IMO - I try to steer clear of Java's date/time API wherever possible.

Having said that, we'd need to know more about the format you're trying to parse, and what's going wrong with SimpleDateFormatter. (As a general rule, if you're "experiencing problems" with something and want those problems fixed, it helps to describe what the problems are, ideally with a short but complete program to demonstrate the problem.)

Jon Skeet
A: 

Best way to serialize the date in javascript is to use toUTCString (not just toString()); toUTCString will produce an rfc 822 date (in the same format as used by http). Then you can just use the following SimpleDateFormat pattern to parse it in java:

new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH)
Justin Ludwig