views:

89

answers:

4

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 SimpleDateFormat 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!

+4  A: 

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

Noel M
... and if that's so, you just need to add .getTime() after the parsing expression in order to use the constructor new Date(Long).
vlood
Good catch! The parse result is already a Date object. But then I'd expected a compile time error, not a runtime problem. Could be a typo in the question.
Andreas_D
that was the problem thanks
schwiz
@schwiz - ??? how did you get an (runtime) exception on the parse call in code that doesn't compile??
Andreas_D
I didn't copy/paste that code I just typed it out to give an example of what I was trying to do.
schwiz
@shwiz - well ... don't!
Stephen C
+2  A: 

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.

Steve McLeod
+1  A: 

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
Rulmeq
But you shouldn't be able to compile something with `new Date(new Date()))`
Andreas_D
A: 

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

Andreas_D