[英]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.