我在尝试以下代码时遇到解析异常:
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)
Input: Sat Jun 01 12:53:10 IST 2013
预期输出:Jun 01,2013 12:53:10
怎么解决这个问题呢?
您的模式根本不对应于输入字符串...它不起作用并不奇怪。这可能会更好:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
然后,要使用所需的格式进行打印,您需要第二个 SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
Notes:
- 您应该包含区域设置,就好像您的区域设置不是英语一样,可能无法识别日期名称
-
IST 不明确,可能会导致问题 https://stackoverflow.com/questions/11264697/java-timezone-strange-behavior-with-ist因此,如果可能的话,您应该在输入中使用正确的时区名称。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)