简体   繁体   English

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

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

I would like to parse a date. 我想解析一个约会。 My String date is "Thu Jan 19 2012 08:00 PM". 我的字符串日期是“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: 调试图片:

在此输入图像描述

Thanks! 谢谢!

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 输出为2012-01-19 13:00:00

Cheers!!! 干杯!!! Happy to help!!! 乐于帮助!!!

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

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

// make sure your string date and DateFormat pattern are same; //确保你的字符串日期和DateFormat模式相同; // for example you have String date="2018/07/10 10:00:00"; //例如你有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"

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. 正如您在此处所见http://ideone.com/MBzYn代码完美无缺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.text.ParseException:无法解析的日期:“ 2015-08-19T00:00:00” - java.text.ParseException: Unparseable date: “2015-08-19T00:00:00” java.text.ParseException:无法解析的日期:“ CET 2012年1月11日星期三00:00:00” - java.text.ParseException: Unparseable date: “Wed Jan 11 00:00:00 CET 2012” java.text.ParseException:无法解析的日期:“ 2012年1月11日08:00:00” - java.text.ParseException: Unparseable date:“01/11/2012 08:00:00” java.text.ParseException:无法解析的日期:“01:19 PM” - java.text.ParseException: Unparseable date: “01:19 PM” 不可解析的日期java.text.ParseException:不可解析的日期:“ 2015-10-08 05:00:00.0” - Unparsable Date java.text.ParseException: Unparseable date: “2015-10-08 05:00:00.0” java.text.ParseException:无法解析的日期:“ 2018年10月7日,星期日,11:00 PM” - java.text.ParseException: Unparseable date: “11:00 PM, Sun 07 Oct 2018” 异常:java.text.ParseException:无法解析的日期:“ 2012年2月15日” - Exception :java.text.ParseException: Unparseable date: “02/15/2012” java:java.text.ParseException:无法解析的日期 - java : java.text.ParseException: Unparseable date java.text.ParseException:无法解析的日期 - java.text.ParseException: Unparseable date java.text.ParseException:无法解析的日期:“ IST” - java.text.ParseException: Unparseable date: “IST”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM