在我的项目中,我试图解析日期格式,如“Mon Oct 20 00:00:00 GMT + 06:30 2014”到dd-MM-yyyy,但我收到以下错误。我希望有人能解决我这个问题。
谢谢,
10-20 13:03:01.390: W/System.err(23409): java.text.ParseException: Unparseable date: "Mon Oct 20 00:00:00 GMT+06:30 2014" (at offset 0)
parseDate.java
SimpleDateFormat formatter_date = new SimpleDateFormat("dd-MM-yyyy");
String sdate="Mon Oct 20 00:00:00 GMT+06:30 2014";
try {
Date _date= formatter_date.parse(sdate);
holder.txtDate.setText(String.valueOf(_date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:8)
使用此代码
public static String parseTodaysDate(String time) {
String inputPattern = "EEE MMM d HH:mm:ss zzz yyyy";
String outputPattern = "dd-MM-yyyy";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = null;
String str = null;
try {
date = inputFormat.parse(time);
str = outputFormat.format(date);
Log.i("mini", "Converted Date Today:" + str);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
答案 1 :(得分:4)
替换
SimpleDateFormat formatter_date = new SimpleDateFormat("dd-MM-yyyy");
与
SimpleDateFormat formatter_date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);