1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user997777
  • 569
  • 1
  • 7
  • 19

3 Answers3

1

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

Cat
  • 66,919
  • 24
  • 133
  • 141
0

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?

Varun Vishnoi
  • 980
  • 1
  • 9
  • 32
0

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));
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • You probably mean, that im idiot, but it doesnt work. I copy the code, try this string time but still it not works. – user997777 Nov 03 '12 at 07:07
  • I tried that but it doesnt work. I am probably making some mistakes. I try to find them and add the correct code for me. – user997777 Nov 03 '12 at 08:00