如何在单击提交按钮上存储创建日期

时间:2014-06-26 08:18:50

标签: jsf

当我单击命令按钮时,如何获取创建日期并将其存储到数据库。

     <p:inputText id="diff" styleClass="daysNumber" value="#{dayDiffBean.diff}">
     <f:valueChangeListener value="#{vacatioBean.vacation.balanceDay}" />
     </p:inputText>
     <h:commandButton styleClass="sndbutton1" value="#{text['employee.send']}"
                action="#{vacationBean.addVac}" />

在Bean中我有

private static final long serialVersionUID = 1L;
private Date vEndDate;
private Date vStartDate;
private Date createdDate;
    private String isNobody;
Requestrelax vacation;
Employee e;
    private short balanceDay;


@EJB
VacationLocal vacations;

@ManagedProperty(value="#{loginBean.userId}")

    Integer userId;

@EJB
EmployeesLocal employees;



@PostConstruct
public void init(){
    System.out.println("0");
    vacation=new Requestrelax();
    e=employees.getEmployee(userId);
    vacation.setEmployee(e);


}

public void addVac(){
    System.out.println("1");
    System.out.println("____________" +createdDate);
    vacation.setCreatedDate(createdDate);

    vacations.addEmployeeVacation(vacation);

}

    public Integer getUserId() {
    return userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

public Employee getE() {
    return e;
}

public void setE(Employee e) {
    this.e = e;
}

public Requestrelax getVacation() {
    return vacation;
}

public void setVacation(Requestrelax vacation) {
    this.vacation = vacation;
}

public Date getvEndDate() {
    return vEndDate;
}

public void setvEndDate(Date vEndDate) {
    this.vEndDate = vEndDate;
}

public Date getvStartDate() {
    return vStartDate;
}

public void setvStartDate(Date vStartDate) {
    this.vStartDate = vStartDate;
}

public short getBalanceDay() {
    return balanceDay;
}

public void setBalanceDay(short balanceDay) {
    this.balanceDay = balanceDay;
}

public Date getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}



 }

当我尝试在控制台上打印它时,如何使用支持bean获取创建日期,它显示为null 任何建议?

2 个答案:

答案 0 :(得分:2)

除了Josef E。的答案之外,我还建议使用Joda Time,因为Java Date API在以前版本的Java中是如何的。

幸运的是,在Java 8中,这个问题已得到解决。

答案 1 :(得分:0)

您的Date对象为null,因为它从未初始化。你需要这样做:

Date createdDate = new Date();

这也是createdDate为其值创建的实际Date的原因。


切线上,我建议在处理日期时使用Calendar。部分原因是Date API主要是 有缺陷的 已弃用

Calendar是一个Singleton对象,可以按如下方式使用:

Calendar createdDate = Calendar.getInstance();

创建此对象时,除非您另行指定,否则它将使用当前LocaleTimeZone

希望有所帮助!