【问题标题】:Date format is not saving correctly日期格式未正确保存
【发布时间】:2017-12-12 02:07:32
【问题描述】:

从事一个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

【问题讨论】:

    标签: spring hibernate date jsp spring-mvc


    【解决方案1】:

    更改日期类型

    private Date joiningDate;
    

    java.sql.Date
    

    【讨论】:

      【解决方案2】:

      您可以使用它:-您可以以毫秒为单位发送时间,然后您可以在 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;
                  }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        • 2012-10-14
        • 2019-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多