Reputation: 3048
I'd like to do this:
Date meetingDateAndTime;
<h:inputText value="Date"/>
<h:inputText value="Time"/>
Once I got only one attribute Date, is it possible to type Date in an inputText and type Time into another and then merge them together?
Upvotes: 5
Views: 9803
Reputation: 62864
I hope you know that most of the methods (and constructors) in the java.util.Date class are deprecated, so I suggest you to use the java.util.GregorianCalendar instead.
Try this:
<h:inputText value="#{myBean.meetingDate}">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>
<h:inputText value="#{myBean.meetingTime}">
<f:convertDateTime pattern="hh:mm:ss" />
</h:inputText>
Here's the backing bean:
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MyBean {
private GregorianCalendar fullMeetingDateTimeInfo = new GregorianCalendar();
private Date meetingDate;
private Date meetingTime;
public Date getMeetingDate() { return meetingDate; }
public Date getMeetingTime() { return meetingTime; }
public Date setMeetingDate(Date meetingDate) {
this.meetingDate = meetingDate;
fullMeetingDateTimeInfo.set(Calendar.YEAR, Integer.parseInt(new SimpleDateFormat("yyyy").format(meetingDate)));
fullMeetingDateTimeInfo.set(Calendar.MONTH, Integer.parseInt(new SimpleDateFormat("MM").format(meetingDate)));
fullMeetingDateTimeInfo.set(Calendar.DATE, Integer.parseInt(new SimpleDateFormat("dd").format(meetingDate)));
}
public Date setMeetingTime(Date meetingTime) {
this.meetingTime = meetingTime;
fullMeetingDateTimeInfo.set(Calendar.HOUR_OF_DAY, Integer.parseInt(new SimpleDateFormat("H").format(meetingTime)));
fullMeetingDateTimeInfo.set(Calendar.MINUTE, Integer.parseInt(new SimpleDateFormat("mm").format(meetingTime)));
fullMeetingDateTimeInfo.set(Calendar.SECOND, Integer.parseInt(new SimpleDateFormat("ss").format(meetingTime)));
}
public GregorianCalendar getFullMeetingDateTimeInfo() {
return fullMeetingDateTimeInfo;
}
}
As you can see, I'm merging the date info from the one Date object with the time info from the other Date object. The merged data is stored in the fullMeetingDateTimeInfo property. So, in further you have to work with the fullMeetingDateTimeInfo property if you'd like to have the complete meeting-time info merged in one object.
Good luck !
Konstantin
Upvotes: 1
Reputation: 32949
You could use two DateFormats
, one that outputs the Date and one that outputs the Time.
SimpleDateFormat
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
Edit: Sorry, I thought you were trying to write two fields from one date not create one date from two fields. What about the following:
DateFormat dateFormat = new SimpleDateFormt("dd-MM-yyyy hh:mm:ss);
String dateString = dateInputText + " " + timeInputText;
Date date = dateFormat.parse(dateString);
Upvotes: 6
Reputation: 1
try this:
<h:inputText value="dateobject">
<f:convertDateTime pattern="dd-MM-yyyy" />
</h:inputText>
<h:inputText value="dateobject">
<f:convertDateTime pattern="hh:MM:ss" />
</h:inputText>
doh, I misunderstood the question :o)
Upvotes: 0