Android java.text.ParseException:无法解析的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26460299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:31:47 来源:igfitidea点击:
java.text.ParseException: Unparseable date
提问by SAWAUNG
In my project, I am trying to parse date format like this "Mon Oct 20 00:00:00 GMT+06:30 2014" to dd-MM-yyyy but I got the following error. I am hoping someone to solve me this problem.
在我的项目中,我试图将像“Mon Oct 20 00:00:00 GMT+06:30 2014”这样的日期格式解析为 dd-MM-yyyy,但出现以下错误。我希望有人能解决我这个问题。
Thanks,
谢谢,
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
解析日期.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();
}
回答by Meenal
use this code
使用此代码
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;
}
回答by cosmincalistru
Replace
代替
SimpleDateFormat formatter_date = new SimpleDateFormat("dd-MM-yyyy");
with
和
SimpleDateFormat formatter_date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);