JSF Converter Timestamp
I want to convert my input into a timestamp value.
I have only found a dateconverter in examples. Are there any best practises?
Thank you
Update:
I want to save a birthday of a user but my backend requires a timestamp value. And I have problems with binding it to my jsf frontend..
Maybe a link to an example would be helful :-)
I tried it as follows:
public void setBday(Date bday) {
member.setBirthday(new Timestamp(bday.getTi开发者_JAVA百科me()));
}
public Timestamp getBday() {
return member.getBirthday();
}
But I get exceptions (strange):
/createMember.xhtml @34,54 value="#{member.bday}": Cannot convert 13.01.83 01:00 of type class java.util.Date to class java.sql.Timestamp
(Is it maybe because of get method?)
Bind to Date#getTime()
instead to get/set the raw timestamp in milliseconds.
<h:inputText value="#{bean.date.time}" />
Or if you want to input/output a human readable date, then just stick to using #{bean.date}
and use the standard date converters.
<h:inputText value="#{bean.date}">
<f:convertDateTime type="date" dateType="short" />
</h:inputText>
In the backend just use Date#getTime()
to process the timestamp.
Update: you shouldn't clutter your model with JDBC specific API. The java.util.Date
represents the timestamp. Use java.sql.Timestamp
only at that point you're about to persist a java.util.Date
in a TIMESTAMP
or DATETIME
column of your DB.
preparedStatement.setTimestamp(index, new Timestamp(bean.getDate().getTime()));
精彩评论