Search code examples
javadateconvertersvaadindatefield

Converting value of DateField in vaadin


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 ?


Solution

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