开发者

Struts2 Java Util date conversion problem

In my application i have one textfield which maps to date in action class. If i entered 15/12/2011 (MM/dd/开发者_如何学Goyyyy) it automatically converts it to 03/12/2012(MM/dd/yyyy).

My code is :

jsp Page :

<s:textfield name="drDt" id="drDt" />

Action class:

private Date drDt;
public Date getDrDt() {
    return drDt;
}

public void setDrDt(Date drDt) {
    this.drDt = drDt;
}

Problem is with code :

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

sdf.parse(dateValue);

above statement has some problem.


This is the "default" behaviour. Otherwise, set dateFormat.setLenient() to false. Setting that to false will throw Exception upon encountering invalid date like the one in your example. You need to handle that gracefully. Perhaps, show a message to the user that the date entered is not correct or invalid. Precisely, add validator to validate user inputs.


MM/dd/yyyy is Month / day / year. You wrote 15/12/2011, that is Month 15 = (15 - 12 ) =3 / day 12 and years + 1 (2011 + the extra year from 15) as 15 is an invalid month. Try switching to dd/MM/yyyy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜