Pra*_*dra 0 java date date-parsing simpledateformat datetime-parsing
我写了一段代码来解析日期字符串-
DateFormat cal = new SimpleDateFormat("yyyy-MM-dd hh:mm");
cal.setLenient(false);
try {
cal.parse("2018-01-01 14:42");
}
catch (Exception e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但我有一个例外说-
java.text.ParseException: Unparseable date: "2018-01-01 14:42"
at java.base/java.text.DateFormat.parse(DateFormat.java:388)
at MyClass.main(MyClass.java:10)
Run Code Online (Sandbox Code Playgroud)
我不确定为什么我看到此错误,因为日期字符串和给定的格式正确。请帮忙
从文档https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html中:
小写SimpleDateFormat字母h以12小时格式表示小时,而24小时格式以大写字母H表示。当14> 12时,无法解析日期14:42。