gpt4 book ai didi

java - 转换 vaadin 中 DateField 的值

转载 作者:行者123 更新时间:2023-12-01 12:45:34 26 4
gpt4 key购买 nike

我目前正在使用 Vaadin 框架。我想转换我的 DateField 中的字符串至Date 。所以我有两个类,一个是 View ,另一个应该包含我通过数据绑定(bind)保存的值。

这是 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);

在同一个类中数据绑定(bind):

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 这样的日期.

我应该怎么做?

最佳答案

您需要在 Bean 类中格式化日期。不在您的查看代码中。

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

关于java - 转换 vaadin 中 DateField 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24733370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com