user997777
user997777

Reputation: 579

Simpledateformat ParseException

I need to change the input date format to my desired format.

String time = "Fri, 02 Nov 2012 11:58 pm CET";
SimpleDateFormat displayFormat = 
    new SimpleDateFormat("dd.MM.yyyy, HH:mm");
SimpleDateFormat parseFormat = 
    new SimpleDateFormat("EEE, dd MMM yyyy HH:mm aa z");
Date date = parseFormat.parse(time);
System.out.println("output is " + displayFormat.format(date));

it gives me this error

java.text.ParseException: Unparseable date: "Fri, 02 Nov 2012 11:58 pm CET"
    at java.text.DateFormat.parse(Unknown Source)
    at Main.main(Main.java:10)

Can anyody help me? Because this code doesn't work.

Upvotes: 1

Views: 1082

Answers (3)

Niranj Patel
Niranj Patel

Reputation: 33238

First of All I must agree with @Eric answer.

You just need to remove "CET" from your string of date.

Here is sample code. Check it.

        String time = "Fri, 02 Nov 2012 11:58 pm CET";
        time = time.replaceAll("CET", "").trim();
        SimpleDateFormat displayFormat = 
            new SimpleDateFormat("dd.MM.yyyy, HH:mm");
        SimpleDateFormat parseFormat = 
            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm aa");
        Date date = null;
        try {
            date = parseFormat.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("output is " + displayFormat.format(date));

Upvotes: 0

Cat
Cat

Reputation: 67502

It appears Android's z does not accept time zones in the format XXX (such as "CET"). (Pulling from the SimpleDateFormat documentation.)

Try this instead:

String time = "Fri, 02 Nov 2012 11:58 pm +0100"; // CET = +1hr = +0100
SimpleDateFormat parseFormat = 
    new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aa Z"); // Capital Z
Date date = parseFormat.parse(time);

SimpleDateFormat displayFormat = 
    new SimpleDateFormat("dd.MM.yyyy, HH:mm");
System.out.println("output is " + displayFormat.format(date));

output is 02.11.2012, 22:58

Note: Also, I think you meant hh instead of HH, since you have PM.

Result is shown here. (This uses Java7's SimpleDateFormat, but Android should support RFC 822 timezones (+0100) as well.)

NB: Also, as it appears Android's z accepts full names ("Pacific Standard Time" is the example they give), you could simply specify "Centural European Time" instead of "CET".

Upvotes: 1

Varun Vishnoi
Varun Vishnoi

Reputation: 990

Try out the following code:

SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMMdd");
    System.out.println(date_format.format(cal.getTime()));

It will work.. If not print the log cat? What erroe is coming?

Upvotes: 0

Related Questions