在vaadin中转换DateField的值 [英] Converting value of DateField in vaadin
问题描述
DateField
中的字符串转换为 Date
。所以我有两个类,一个是视图,另一个应该包含我用数据绑定保存的值。 这是视图中的DateField:
timestart = new DateField();
timestart.setId(timestart);
timestart.setDateFormat(yyyy-MM-dd HH:mm);
timestart.setValue(new Date());
timestart.setResolution(Resolution.MINUTE);
timestart.setConverter(XXX); //这里我不知道该怎么做
layout.addComponent(timestart,3,2);
在同一个类中,数据绑定:
binder.bind(timestart,timestart);
//这部分工作
在我的另一个类中:
private Date timestart;
我想将这个 timestart
保存在数据库,所以我需要一个格式化的值,如上面 yyyy-MM-dd HH:mm
,但是当我没有 timestart.setConverter
我得到一个日期像 Wed Jul 16 11:11:00 CEST 2014
。
我这样做
您需要在您的bean类中格式化Date。不在您的查看代码中。
private SimpleDateFormat dateFormat;
private Date timestart;
public ConstructorOfYourClass {
timestart = new Date(); //默认日期
//您的优先日期格式
dateFormat = new SimpleDateFormat(yyyy-MM-dd hh:mm);
}
// ...其他代码...
//您的日期的Getter方法
public String getdateFormat ){
return dateFormat.format(timestart);
}
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 ?
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);
}
这篇关于在vaadin中转换DateField的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!