This is a problem that colleagues encountered.
The Date in the code, the format placed on the page is "Fri Mar 21 09:20:38 CST 2014" (not displayed, just for passing to the next controller).
When submitting the form again, the private Date startTime of the Dto class; is not set to the value.
I did some experiments with local programs
public static void main(String[] args) { Date now = new Date(); System.out.println(now); String nowStr = now.toString(); DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); Date parsedNow = null; try { parsedNow = format.parse(nowStr); System.out.println(parsedNow); } catch (ParseException e) { e.printStackTrace(); } }An error occurred when the program executes format.parse(nowStr)
Java.text.ParseException: Unparseable date: "Fri Mar 21 09:25:48 CST 2014"
at java.text.DateFormat.parse(DateFormat.java:337)
After analyzing and viewing the source code, we draw conclusions that errors caused by the language used by the system.
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");The default is actually
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", new Locale(System.getProperty("user.language")));Among them, System.getProperty("user.language") is Chinese because the system is zh, and this format should not be supported in the Chinese time zone.
Modify the above code to verify this point
public static void main(String[] args) { Date now = new Date(); System.out.println(now); String nowStr = now.toString(); DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", new Locale(System.getProperty("user.language"))); System.out.println(System.getProperty("user.language")); Date parsedNow = null; try { parsedNow = format.parse(nowStr); System.out.println(parsedNow); } catch (ParseException e) { format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH); try { System.out.println("new format by 'en'"); System.out.println(format.parse(nowStr)); } catch (ParseException e1) { e1.printStackTrace(); } } }Another solution is to convert the date format once in the jsp page, such as
<input type="hidden" name="data" value=' <fmt:formatDate value="${dto.date}" pattern="yyyy-MM-dd"/> '/> The above is all the content of this article. I hope that the content of this article will be of some help to everyone’s study or work. I also hope to support Wulin.com more!