给定输入日期字符串,我想解析输入日期字符串。这个输入日期格式最后我想改成其他日期格式。但是目前我正在解析输入字符串本身时遇到异常。
String startDateString = " Jun 28, 2015T12:00:00 AM";
DateFormat df = new SimpleDateFormat("MMM dd, yyyy'T'hh:mm:ss a");
java.util.Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
}
Getting the following exception,
java.text.ParseException: Unparseable date: " Jun 28, 2015T12:00:00 AM"
at java.text.DateFormat.parse(DateFormat.java:357)
at promo.json.parser.handler.Test.main(Test.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
答案 0 :(得分:5)
您的输入值" Jun 28, 2015T12:00:00 AM"
与解析格式不匹配,因为之前的前导空格字符
Jun
。
尝试将字符串编辑为:
String startDateString = "Jun 28, 2015T12:00:00 AM";
或致电String::trim()
:
startDateString.trim();