日期格式无法正确保存

时间:2017-12-12 02:07:33

标签: spring hibernate date jsp spring-mvc

处理spring MVC项目以及我从JSP输入的日期是

在Bean类中 它是

@DateTimeFormat(pattern = "MM-dd-yyyy") 
        @Temporal(TemporalType.DATE)
        @Column(name="Joining_Date")
        private Date joiningDate; // in jsp as


<td><form:label path="joiningDate">Client Joining Date:</form:label></td>
<td><form:input type="date" path="joiningDate"  value="${client.joiningDate}"/><span id="joiningDateError" ></span></td>

控制器代码

@InitBinder     
public void initBinder(WebDataBinder binder){
     binder.registerCustomEditor(Date.class,     
                         new CustomDateEditor(new SimpleDateFormat("MM-dd-yyyy"), true, 10)); 
}

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveClient(@ModelAttribute("command") ClientBean clientBean, BindingResult result) {

    System.out.println(clientBean.getJoiningDate());

    if(result.hasErrors()){
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("clients", prepareListofBean(clientService.listClients()));
        return new ModelAndView("addClient", model);
        //return model;
    }
  

我输入的日期是12/28/2017(MM-dd-yyyy),但输入为0196-01-12

2 个答案:

答案 0 :(得分:0)

更改

中的日期类型
private Date joiningDate;

java.sql.Date

答案 1 :(得分:0)

您可以使用它: - 您可以以秒为单位发送时间,然后您可以在jsp侧解析     Java代码应该使用正确的import java.util.Date。

public static String getSimpleDateFormat(long timeInMills) {

    SimpleDateFormat output = new SimpleDateFormat("dd-MMMM-yyyy HH:mm:ss");
    String formattedTime = output.format(timeInMills);

    return formattedTime;
}

use at jsp side :-pass the date 

like var joinDate =formatIntDate(joiningDate);

function formatIntDate(intDate) {

try {
    if (intDate && isInt(intDate))
        return formatDate(new Date(parseInt(intDate)));
    else
        return intDate;
} catch (e) {
    console.log("intDate: " + intDate);
    return intDate;
            }
}