java.text.ParseException:无法解析的日期:"Thu Jan 19 2012 08:00 PM"

Jos*_*dez 7 java timestamp date

我想解析一个约会.我的字符串日期是"Thu Jan 19 2012 08:00 PM".我要解析的代码是:

format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa");
this.settDate(new Timestamp((format.parse(sDate)).getTime()));
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用.我怎么解析这个日期?

完整的方法是:

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);
}
Run Code Online (Sandbox Code Playgroud)

例外:

java.text.ParseException: Unparseable date: "Thu Jan 19 2012 01:00 AM"
Run Code Online (Sandbox Code Playgroud)

调试图片:

在此输入图像描述

谢谢!

Fah*_*kar 17

尝试以下代码...经过测试和工作

    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);
Run Code Online (Sandbox Code Playgroud)

输出是 2012-01-19 13:00:00

干杯!!!乐于帮助!!!


Shi*_*gon 5

在这种情况下,请尝试设置美国等语言环境:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa",Locale.US);
format.parse("Thu Jan 19 2012 08:00 PM");
Run Code Online (Sandbox Code Playgroud)