Simpledateformat ParseException

前端 未结 3 354
慢半拍i
慢半拍i 2021-01-23 09:49

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 SimpleDat         


        
相关标签:
3条回答
  • 2021-01-23 10:16

    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?

    0 讨论(0)
  • 2021-01-23 10:19

    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));
    
    0 讨论(0)
  • 2021-01-23 10:22

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

    0 讨论(0)
提交回复
热议问题