Peter Penzov
Peter Penzov

Reputation: 1658

Convert Date into Timestamp

I have a JSF page with two input fields - date and hour:

<tr>
    <td>Planned Maintenance Stop Date</td>                                        
    <p:calendar pattern="dd-MM-yyyy" value="#{DCProfileTabGeneralController.date1}"/>
</tr>
<tr>
    <td>Planned Maintenance Stop Hour</td>
    <td>
        <h:inputText value="#{ud.maintenanceStopHour}"/>
    </td>
</tr>

Primefaces uses Date Object to store the date when the date is selected from the calendar. The question is how I can store the date from the calendar and the hour entered manually from the second input filed as a Timestamp into the database.

private Date maintenanceStartDate;         
private Date maintenanceStartHour;

How I can convert two dates into java Timestamp?

Upvotes: 0

Views: 2764

Answers (2)

Mahendra
Mahendra

Reputation: 333

Easiest approach is to use

<p:calendar value="#{calendarBean.date1}" pattern="MM/dd/yyyy HH:mm" />

Date with time will be stored in Date object in managed bean. Which you can convert into timestamp easily, as below.

new Timestamp(date1.getTime());

Upvotes: 0

Petr Mensik
Petr Mensik

Reputation: 27496

You can always convert Date to Timestamp with new Timestamp(date.getTime());

or you can specify you own converter like

<p:calendar>
    <f:converter converterId="timestampConverter" />
</p:calendar>

which will do the job.

Upvotes: 1

Related Questions