开发者

SimpleDateFormatter won't parse!

Hello I am trying to use the SimpleDateFormatter to parse the date Wed, 30 Jun 2010 15:07:06 CST

I am using the following code

public static SimpleDateForm开发者_运维问答at postedformat = 
    new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date newDate = new Date(posteformat.parse("Wed, 30 Jun 2010 15:07:06 CST"));

but I am getting an illegalArgumentException. Please help!


postedformat.parse() returns a Date, and there is no Date(Date) constructor.

Presumably removing the call to new Date, so you say Date newDate = poste.... will suffice


Your code fragment doesn't compile. This slight modification compiles and parses successfully:

public static void main(String[] args) throws ParseException {
    SimpleDateFormat postedformat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
    Date newDate = postedformat.parse("Wed, 30 Jun 2010 15:07:06 CST");
    System.out.println("newDate = " + newDate);
}

This is using Java 6 on Mac OS X.


There is no java.util.Date() constructor that takes a java.util.Date as an argument

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormat {
    public static SimpleDateFormat postedformat = 
        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
    public static void main(String[] args) {
        try {
            Date newDate = postedformat.parse("Wed, 30 Jun 2010 15:07:06 CST");
            System.out.println("Date: " + newDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Outputs:

Date: Wed Jun 30 22:07:06 BST 2010


The javadoc examples shows unescaped comma but for the US locale. So either try escaping the comma (as Aaron suggested) or use the other constructor and set the Locale:

new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);

Another problem could be the timezone ('CST') which is deprecated on the on hand and ambigious on the other (as per javadoc of java.util.TimeZone). Test, if it works without the timezone attribute (in both the format String and the value).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜