tags:

views:

99

answers:

6

Hi,

I am getting date in following format, as java string:

Sat Jan 01 00:00:00 CET 2000

i want to convert it to yyyy-MM-dd fromat. For this i am doing:

String strDate = "Sat Jan 01 00:00:00 CET 2001";
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
try{
Date parsed = sdf.parse(strDate);
}catch(Exception e){
System.out.println("Exception: " + e.getMessage());
}

But i am getting exception: Unparseable date: "Sat Jan 01 00:00:00 CET 2001"

Please give me some solution for this.

Thank you

+7  A: 

The SimpleDateFormat needs to be the input date format, then you can use another SimpleDateFormat to get the appropriate output format, i.e.:

String strDate = "Sat Jan 01 00:00:00 CET 2001";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
SimpleDateFormat outputDate = new SimpleDateFormat("yyyy-MM-dd");
try{
    Date parsed = sdf.parse(strDate);
}
catch(Exception e){
    System.out.println("Exception: " + e.getMessage());
}
System.out.println("Date: " + outputDate.format(parsed));
Lazarus
Hi,String temp = "Sat Jan 01 00:00:00 CET 2000";SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); SimpleDateFormat outputDate = new SimpleDateFormat("yyyy-MM-dd"); Date parsed = null;try{ parsed = sdf.parse(temp); } catch(Exception e){ System.out.println("Exception: " + e.getMessage()); } System.out.println("Date: " + outputDate.format(parsed));
Ajay
I think my edit must have passed your comment like ships in the night ;)
Lazarus
sorry for worng format. I made changes as per your code, i am still getting exception
Ajay
@Ajay, try the input string with one 'z' as now shown above.
Lazarus
hi Lazarus, i tried with one 'z' also but still getting exception
Ajay
This won't compile unless the last System.out.println statement is moved within the try block, but I verified it works.
gregcase
@gregcase: it does not work for me, as my locale is nl_BE, and 'Sat' should be 'zat' for me :)
Fortega
@Fortega SimpleDateFormat has a constructor that accepts a Locale as a second argument, that will allow you to do Locale specific parsing and formatting.
gregcase
@gregcase: yup, I know. See my answer :-)@Ajay: does it work for you when you use a Locale.US as second argument when creating the 'sdf' object?
Fortega
+3  A: 

You are trying to parse the date with the format "yyyy-MM-dd".

You need to build a SimpleDateFormat matching the format you need to parse, then use the one in your snippet to format it.

The parse format you need will be something along the lines of "EEE MMM dd HH:mm:ss z yyyy".

Brabster
+3  A: 

You need a SimpleDateFormat to parse the existing string and another SimpleDateFormat to format the parsed Date object into the appropriate yyyy-MM-dd string.

You can use applyPattern to change the current pattern in the SimpleDateFormat so you don't have to create a new object.

I'll leave the patterns as an exercise for the student.

tKe
+1  A: 

Your pattern needs to match the date you are parsing, something like this: Look at the SimpleDateFormat doc for complete instructions.

SimpleDateFormat sdf = new SimpleDateFormat ("EEE MMM dd hh:mm:ss zzz yyyy");
willcodejavaforfood
A: 

What Date class are you using? If you're using an IDE, it's easy to accidentally use java.sql.Date instead of java.util.Date by accident. Here's my complete sample program and its output:

package pkg;

import java.lang.String;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Foo
{
    public static void main(String[] args) throws Exception
    {
        Date parsed = null;
        String strDate = "Sat Jan 01 00:00:00 CET 2001";
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
        SimpleDateFormat outputDate = new SimpleDateFormat("yyyy-MM-dd");
        try{
            parsed = sdf.parse(strDate);
        }
        catch(Exception e){
            System.out.println("Exception: " + e.getMessage());
        }
        System.out.println("Date: " + outputDate.format(parsed));
    }
}

produced

Date: 2000-12-31

which is correct, as I'm in EST, unless I'm mistaken about where CET is. No exceptions encountered, of the parsing kind or otherwise.

Lord Torgamus
A: 

If your locale is not US or another english language locale, you will get an exception, because Sat is not known, so you should specify the locale in your input format.

Try this:

String dateAsString = "Sat Jan 01 00:00:00 CET 2001";
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Date: " + outputFormat.format(inputFormat.parse(dateAsString)));

ps: Jan 01 2001 is not a Saturday :)

Fortega