简体   繁体   English

从日期分割日期和时间单独输入

[英]Split Date and Time from a Date to type separately

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? 一旦我只得到一个属性Date,是否可以在inputText中键入Date并在另一个中键入Time然后将它们合并在一起?

You could use two DateFormats , one that outputs the Date and one that outputs the Time. 您可以使用两个DateFormats ,一个输出Date,另一个输出Time。 SimpleDateFormat 的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); 

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. 我希望您知道java.util.Date类中的大多数方法(和构造函数)都已弃用,因此我建议您改用java.util.GregorianCalendar。

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: 这是支持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. 如您所见,我将来自一个Date对象的日期信息与来自另一个Date对象的时间信息合并。 The merged data is stored in the fullMeetingDateTimeInfo property. 合并的数据存储在fullMeetingDateTimeInfo属性中。 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. 因此,如果您希望将完整的会议时间信息合并到一个对象中,则必须使用fullMeetingDateTimeInfo属性。

Good luck ! 祝好运 !

Konstantin 康斯坦丁

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) 啊,我误解了这个问题:o)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM