JSF Converter Timestamp

11,378

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()));
Share:
11,378
Sven
Author by

Sven

Hi, I am a student from Germany. I set my focus on Web Application Development, especially with Java. And I hope this awesome site can help with the awkward behaviour I sometimes cause (: Have a nice day.

Updated on June 04, 2022

Comments

  • Sven
    Sven almost 2 years

    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.getTime()));
            }
            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?)