Simpledateformat ParseException

前端 未结 3 355
慢半拍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: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".

提交回复
热议问题