最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Parsing JavaScript Date string in Java - Stack Overflow

programmeradmin6浏览0评论

A JavaScript client sends some strings to my server, one of which es 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)

A JavaScript client sends some strings to my server, one of which es 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)

Share Improve this question edited Feb 12, 2013 at 20:10 Octavian Helm 39.6k19 gold badges99 silver badges102 bronze badges asked Sep 11, 2009 at 9:18 gotch4gotch4 13.3k29 gold badges111 silver badges177 bronze badges 2
  • What are your problems with SimpleDateFormatter? – Tim Büthe Commented Sep 11, 2009 at 9:21
  • Try moment.js, trust me you'll get rid of all these annoying issues. – Andres Elizondo Commented Dec 13, 2016 at 15:54
Add a ment  | 

3 Answers 3

Reset to default 12

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 plete program to demonstrate the problem.)

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is remended to stop using them pletely 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 pliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

发布评论

评论列表(0)

  1. 暂无评论