【问题标题】:Converting value of DateField in vaadin在vaadin中转换DateField的值
【发布时间】:2014-09-04 04:23:15
【问题描述】:

我目前正在使用 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 ); // Here i don't know what to do
layout.addComponent(timestart, 3, 2);

同一个类中的数据绑定:

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

在我的另一堂课上:

private Date timestart;

我想将这个timestart 保存在数据库中,所以我需要一个像上面yyyy-MM-dd HH:mm 这样的格式化值,但是当我在没有timestart.setConverter 的情况下这样做时,我会得到一个像Wed Jul 16 11:11:00 CEST 2014 这样的日期。

我应该怎么做?

【问题讨论】:

  • 这个字段/列的数据库类型是什么?

标签: java date converter vaadin datefield


【解决方案1】:

您需要在 bean 类中格式化 Date。不在您的查看代码中。

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);
}

【讨论】:

  • 就是这样。谢谢!
  • 附加信息:您的 bean 类中需要两个 getter。其中之一应返回非格式化值。否则活页夹将无法工作。
猜你喜欢
  • 2019-05-14
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多