我将日期作为JSP的输入,然后我需要使用hibernate存储在数据库中。我的日期格式也正确,但仍然无法正常工作。这是我的代码:
String dateStr = request.getParameter("receive_date");
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);
System.out.println(date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.DATE) + "/"+ (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
out.println("formatedDate : " + formatedDate);
尝试更改这两行
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);
至
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
Date date = LocalDate.parse(dateStr, formatter);