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

63,035

Solution 1

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!!!

Solution 2

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

Solution 3

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);
Share:
63,035
Jose Hdez
Author by

Jose Hdez

José L. Hernández received the Telecommunication Engineer's Degree in 2008 and the MEng in the Electronics and Communications program in 2010, both from the University of Valladolid (Spain). He has been working for two years in CARTIF where he develops tasks related to R&D and project management in several European projects. He is expertise in communication protocols, ICT solutions for energy efficiency (middleware, Building Management Systems, control algorithms), database technologies, monitoring and control systems, automation networks, integration of renewable energy resources in buildings and integration and communication between BIM Server and ICT systems.

Updated on July 11, 2020

Comments

  • Jose Hdez
    Jose Hdez almost 4 years

    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 aaa");
    this.settDate(new Timestamp((format.parse(sDate)).getTime()));
    

    However, it does not work. How could I parse this date?

    Complete method is:

    public void saveTask(int iDevice, String description, String sDate) throws ParseException {
        format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa");
        this.setiDeviceId(iDevice);
        this.setsDescription(description);
        this.settDate(new Timestamp((format.parse(sDate)).getTime()));
        DatabaseManager.save(this);
    }
    

    And exception:

    java.text.ParseException: Unparseable date: "Thu Jan 19 2012 01:00 AM"
    

    Debug picture:

    enter image description here

    Thanks!

  • Jesper
    Jesper over 12 years
    @Andrey Yes, but Jose Hdez's default locale is probably not US or UK, so he gets an error if he doesn't specify which locale should be used.