Converting value of DateField in vaadin

一个人想着一个人 提交于 2019-12-11 17:48:14

问题


I'm working with the Vaadin Framework at the moment. I want to convert the string in my DateField to Date. So I have two classes, one is the view and the other should contain the values which I save with data binding.

This is the DateField in the view:

timestart = new DateField("");
timestart.setId("timestart");
timestart.setDateFormat("yyyy-MM-dd HH:mm");
timestart.setValue(new Date());
timestart.setResolution(Resolution.MINUTE);
timestart.setConverter( XXX ); // Here i don't know what to do
layout.addComponent(timestart, 3, 2);

In the same class the data binding:

binder.bind(timestart, "timestart");
//This part is working

And in my other class:

private Date timestart;

I want to save this timestart in a database, so i need a formatted value like above yyyy-MM-dd HH:mm but when I do it without timestart.setConverter I am getting a date like Wed Jul 16 11:11:00 CEST 2014.

How should I do this ?


回答1:


You need to format the Date in your bean class. Not in your View code.

private SimpleDateFormat dateFormat;
private Date timestart;

public ConstructorOfYourClass{
    timestart = new Date(); //Default date
    //Your prefered date format
    dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm"); 

} 
//... other Code ...


//Getter method of your Date
public String getdateFormat(){
return dateFormat.format(timestart);
}


来源:https://stackoverflow.com/questions/24733370/converting-value-of-datefield-in-vaadin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!