简体   繁体   English

无法使用@DateTimeFormat(pattern =“dd / MM / yyyy”)在java.util.Date中转换字符串

[英]Failed to convert string in java.util.Date with @DateTimeFormat(pattern=“dd/MM/yyyy”)

I have a simple POJO with a Date field. 我有一个带有Date字段的简单POJO I want to bind the object with values from a form. 我想用一个表单中的值绑定对象。
In the form I'm using jquery ui datepicker with date format("dd/mm/yyyy") 在表格中我使用日期格式的jquery ui datepicker (“dd / mm / yyyy”)
I have console.log the value and is a string : 13-11-2014 我有console.log的值,是一个字符串:13-11-2014
I'm using spring 4.0.7 我正在使用spring 4.0.7
I have on my dependencies joda-time 2.5 我有我的依赖joda-time 2.5

I get this exception: 我得到这个例外:

Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '22-11-2014'; nested exception is java.lang.IllegalArgumentException: Invalid format: "22-11-2014" is malformed at "-11-2014"]

My POJO: 我的POJO:

    package gr.gsis.announcement.model;


    import java.io.Serializable;
    import java.util.Date;

    import org.springframework.format.annotation.DateTimeFormat;

    public class Announcement implements Serializable{

        private static final long serialVersionUID = -1984554807132781312L;

        private int id;
        private String title;
        private String bodyText;

        @DateTimeFormat(pattern = "dd/MM/yyyy")
        private Date startDate;

        @DateTimeFormat(pattern = "dd/MM/yyyy")
        private Date endDate;

        private boolean activeFlag;

        public Announcement() {

        }

        public Announcement(int id, String title, Date startDate, Date endDate,
                boolean activeFlag) {
            this.id = id;
            this.title = title;
            this.startDate = startDate;
            this.endDate = endDate;
            this.activeFlag = activeFlag;
        }


        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getBodyText() {
            return bodyText;
        }

        public void setBodyText(String bodyText) {
            this.bodyText = bodyText;
        }

        public Date getStartDate() {
            return startDate;
        }

        public void setStartDate(Date startDate) {
            this.startDate = startDate;
        }

        public Date getEndDate() {
            return endDate;
        }

        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }

        public boolean getActiveFlag() {
            return activeFlag;
        }

        public void setActiveFlag(boolean activeFlag) {
            this.activeFlag = activeFlag;
        }

        @Override
        public String toString() {
            return "Announcement [id=" + id + ", title=" + title + ", bodyText="
                    + bodyText + ", startDate=" + startDate + ", endDate="
                    + endDate + ", activeFlag=" + activeFlag + "]";
        }

My controller: 我的控制器:

    package gr.gsis.announcement.controller;

    import gr.gsis.announcement.model.Announcement;

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @Controller
    public class AnnouncementController {

        @RequestMapping("/")
        public String welcome(Model model) {
            model.addAttribute("greeting", "Welcome");

            return "welcome";

        }

        @RequestMapping(value="/create", method = RequestMethod.GET)
        public String getAnnouncementForm(Model model) {

            Announcement announcement = new Announcement();

            model.addAttribute("announcement", announcement);

            return "announcementForm";
        }

        @RequestMapping(value = "/create", method = RequestMethod.POST)
        public String processAnnouncement(@ModelAttribute("announcement") Announcement announcement) {

            System.out.println(announcement);

            return "announcementForm";
        }
    }

my Form JSP: 我的表单JSP:

    <h2>Insert Announcement</h2>

        <form:form method="post" commandName="announcement">

            <div>
                <form:label path="title">Title</form:label>
                <form:input path="title" name="title" type="text"/>
            </div>

            <div>
                <form:label path="startDate">Start Date</form:label>
                <form:input path="startDate" cssClass="datepicker start"  name="startDate" type="text"/>
            </div>

            <div>
                <form:label path="endDate">End Date</form:label>
                <form:input path="endDate" cssClass="datepicker end" name="endDate" type="text"/>
            </div>

            <div>
                <form:label path="activeFlag">Active</form:label>
                <form:checkbox path="activeFlag" name="activeFlag"/>
            </div>


            <div>
                <input type="submit" class="button" value="Save" />
            </div>

        </form:form>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
    <script>
        $(function() {
            $( ".datepicker" ).datepicker({ dateFormat: 'dd-mm-yy' });


        });

What am I doing wrong? 我究竟做错了什么?

Thanks in advance 提前致谢

Your date format does not match input 您的日期格式与输入不匹配

@DateTimeFormat(pattern = "dd/MM/yyyy")

should be 应该

@DateTimeFormat(pattern = "dd-MM-yyyy")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用@DateTimeFormat(pattern =“ HH:mm”)无法将类型[java.lang.String]的属性值转换为所需的类型[java.util.Date] - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] with @DateTimeFormat(pattern = “HH:mm”) 以(yyyy-MM-dd)格式将String转换为java.util.Date,返回类型为Date - Convert String to java.util.Date in the (yyyy-MM-dd) format with return type of Date 将java.util.Date转换为yyyy-MM-dd格式的String,而不创建大量对象 - Convert java.util.Date to String in yyyy-MM-dd format without creating a lot of objects 将YYYY-MM-DD HH:MM:SS格式的java.util.Date转换为与MySQL DATETIME兼容 - Convert java.util.Date in YYYY-MM-DD HH:MM:SS format to compatible with MySQL DATETIME 将格式为 yyyy-MM-dd 的字符串日期解析为 java.util.Date 格式为 yyyy-MM-dd - Parse String date of format yyyy-MM-dd to java.util.Date of format yyyy-MM-dd 如何将dd / MM / yyyy格式的java.sql.Date转换为java.util.Date? - How to convert java.sql.Date to java.util.Date in dd/MM/yyyy format? java.util.Date 格式转换 yyyy-mm-dd 到 mm-dd-yyyy - java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy 如何将 java.util.Date 转换为 soap 支持的日期格式“yyyy-MM-dd'T'HH:mm:ss”与区域 id - How to convert java.util.Date to soap suppported date format “yyyy-MM-dd'T'HH:mm:ss” with zone id 如何将 YY/MM/DD 类型的字符串转换为 java.util.Date? - How to convert string of type YY/MM/DD to java.util.Date? 使用格式MM / dd / yyyy将String Date对象转换为util.Date对象 - Convert String Date object to util.Date object with pattern MM/dd/yyyy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM