我想解析一个日期。我的字符串日期是“清华2012年1月19日下午8:00”。我要解析的代码是:
format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa");
this.settDate(new Timestamp((format.parse(sDate)).getTime()));
然而,它不起作用。我如何解析这个日期呢?
完整的方法是:
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);
}
和例外:
java.text.ParseException: Unparseable date: "Thu Jan 19 2012 01:00 AM"
调试图片:
谢谢!
发布于 2012-01-18 22:35:03
尝试下面的代码...经过测试和工作
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);
输出为2012-01-19 13:00:00
干杯!很乐意为您效劳!
发布于 2012-01-18 21:52:06
尝试设置区域设置,例如本例中的US:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa",Locale.US);
format.parse("Thu Jan 19 2012 08:00 PM");
发布于 2012-01-18 21:46:00
您的默认语言环境是什么?由于日期字符串为英语,请尝试使用美国语言环境对其进行解析:
DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm a", Locale.US);
https://stackoverflow.com/questions/8911099
复制相似问题