有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java date给我的格式是mm/dd/yyyy,其中jquery datepicker的日期格式是dd/mm/yyyy

我对日期格式有一个奇怪的问题。 比如,我使用的jquery日期选择器设置为dd/mm/yy格式。 所以,当我用2015年9月1日填充该字段时,它应该意味着日期是2015年9月1日。 但是,我看到,当我收到控制器中存档的日期时,实际日期看起来像2015年1月9日星期五00:00:00 IST。这不是我想要的,我想要的是日期 2015年9月1日。请帮我弄清楚我遗漏了什么。 下面是代码片段供您参考- jsp

 function() {
        $(".datepicker").datepicker({
            dateFormat: "dd/mm/yy",
            maxDate : 0,
            changeMonth : true,//this option for allowing user to select month
            changeYear : true, //this option for allowing user to select from year range
            showOn : "button",
            buttonImage : "<c:url value="resources/images/calendar.gif"/>",
            buttonImageOnly : true,
            buttonText : "Select date",
            showOtherMonths : true,
            selectOtherMonths : true
        })

    });

<form:form method="post" id="inbound_form" modelAttribute="formObject" action="submit" onsubmit="return validateInboundAdd()" >
<form:input class="datepicker required" style="height:14px"
                                id="dateReceived" size="10" maxlength="10"  type="text" readonly="readonly" value="${dateReceivedContinue}"
                                path="dateReceived" onfocus="inputFocus(this)" onblur="inputBlur(this)"/>

控制器

@RequestMapping(value = "/submit" , method = RequestMethod.POST , params="Save ")
    public String submit(@ModelAttribute("formObject") modelObject modelObject,

    System.out.println("received date -"+modelObject.getDate_received());  // This is giving me the mm/dd/yyyy output, which I want as dd/mm/yyyy. This is getter of the date field 'dateReceived'

共 (1) 个答案

  1. # 1 楼答案

    在ModelFormObject中的date_received字段上定义模式,以便spring能够正确处理转换

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

    希望它能起作用