A JavaScript client sends some strings to my server, one of which comes in form of a JavaScript Date object's string representation.
Now, this JavaScript Date object has its own formatting and I was just wondering if there is a class that does the right conversion, as I am experiencing problems with the SimpleDateFormatter
.
This is how a JavaScript Date string looks like: Tue Feb 12 2013 21:12:28 GMT+0100 (CET)
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Also, quoted below is a notice from the home page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
Solution using java.time
, the modern Date-Time API:
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "Tue Feb 12 2013 21:12:28 GMT+0100 (CET)";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("E MMM d u H:m:s VVZ (z)", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
System.out.println(zdt);
}
}
Output:
2013-02-12T21:12:28+01:00[Europe/Paris]
ONLINE DEMO
For any reason, if you need to convert this object of ZonedDateTime
to an object of java.util.Date
, you can do so as follows:
Date date = Date.from(zdt.toInstant());
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
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)
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With