Simpledateformat ParseException

I need to change the input date format to the 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));

he 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 anyone help me? Because this code does not work.

+1
source share
3 answers

Appears Android zdoes not accept time zones in XXX format (for example, CET). (Pulling out the documentationSimpleDateFormat .)

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

conclusion - 02.11.2012, 22:58

Note. In addition, I think you meant hhinstead hh, as you have PM.

The result is shown here . (This uses Java7 SimpleDateFormat, but Android should also support RFC 822 ( +0100) time slots .)

NB. , , Android z (, " " ), "Centural European Time" "CET".

+1

:

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

. -? erroe?

0

First of all, I have to agree with @Eric's answer.

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

Here is a sample code. Check it out.

        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));
0
source

Source: https://habr.com/ru/post/1652136/


All Articles