java.text.ParseException:无法解析的日期 [英] java.text.ParseException: Unparseable date
问题描述
我在尝试以下代码时遇到解析异常:
I am getting a parsing exception while I am trying the following code:
String date="Sat Jun 01 12:53:10 IST 2013";
SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
Date currentdate;
currentdate=sdf.parse(date);
System.out.println(currentdate);
异常:
线程main"中的异常 java.text.ParseException:无法解析的日期:Sat Jun 01 12:53:10 IST 2013"在 com.ibm.icu.text.DateFormat.parse(DateFormat.java:510)
Exception in thread "main" java.text.ParseException: Unparseable date: "Sat Jun 01 12:53:10 IST 2013" at com.ibm.icu.text.DateFormat.parse(DateFormat.java:510)
输入:Sat Jun 01 12:53:10 IST 2013
预期输出:Jun 01,2013 12:53:10
如何解决这个问题?
推荐答案
你的模式与输入字符串根本不对应......它不起作用并不奇怪.这可能会更好:
Your pattern does not correspond to the input string at all... It is not surprising that it does not work. This would probably work better:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
然后要使用您需要的格式打印,您需要第二个 SimpleDateFormat:
Then to print with your required format you need a second SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
注意事项:
- 您应该包含语言环境,就好像您的语言环境不是英语一样,可能无法识别日期名称
- IST 模棱两可,可能导致问题,因此您应该使用如果可能,请在您的输入中输入正确的时区名称.
- you should include the locale as if your locale is not English, the day name might not be recognised
- IST is ambiguous and can lead to problems so you should use the proper time zone name if possible in your input.
这篇关于java.text.ParseException:无法解析的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!