java.text.ParseException: Unparseable date: “Thu Jan 19 2012 08:00 PM”

后端 未结 5 2202
春和景丽
春和景丽 2020-12-10 16:46

I would like to parse a date. My String date is \"Thu Jan 19 2012 08:00 PM\". And my code to parse is:

format = new SimpleDateFormat(\"EEE MMM dd yyyy hh:mm          


        
相关标签:
5条回答
  • 2020-12-10 16:53

    // make sure your string date and DateFormat pattern are same; // for example you have String date="2018/07/10 10:00:00";

                                DateFormat format = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss");
    
                                // now makesure format has same pattern in this case ""yyyy/MM/dd HH:mm:ss"
    
    0 讨论(0)
  • 2020-12-10 16:56

    Try below code... Tested and worked

        String dateStr = "Thu Jan 19 2012 01:00 PM";
        DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa");
    
        DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
           date = readFormat.parse( dateStr );
        } catch ( ParseException e ) {
            e.printStackTrace();
        }
    
        String formattedDate = "";
        if( date != null ) {
        formattedDate = writeFormat.format( date );
        }
    
        System.out.println(formattedDate);
    

    Output is 2012-01-19 13:00:00

    Cheers!!! Happy to help!!!

    0 讨论(0)
  • 2020-12-10 16:57

    What is your default locale? Since the date string is in English, try parsing it with the US locale:

    DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm a", Locale.US);
    
    0 讨论(0)
  • 2020-12-10 17:02

    Try setting a Locale such as US in this case:

    SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa",Locale.US);
    format.parse("Thu Jan 19 2012 08:00 PM");
    
    0 讨论(0)
  • 2020-12-10 17:10

    Make sure that value really got to that call. Put a breakpoint right on that line and double check actual value.

    As you can see here http://ideone.com/MBzYn code works perfectly.

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