2016-05-15 14 views

Antwort

4

scheint wie ein Vaadin Converter der Weg zu gehen ist:

package org.raubvogel.fooddiary.util; 

import java.time.LocalDateTime; 
import java.time.ZoneOffset; 
import java.util.Date; 
import java.util.Locale; 

import com.vaadin.data.util.converter.Converter; 

/** 
* Provides a conversion between old {@link Date} and new {@link LocalDateTime} API. 
*/ 
public class LocalDateTimeToDateConverter implements Converter<Date, LocalDateTime> { 

    private static final long serialVersionUID = 1L; 

    @Override 
    public LocalDateTime convertToModel(Date value, Class<? extends LocalDateTime> targetType, Locale locale) 
      throws com.vaadin.data.util.converter.Converter.ConversionException { 

     if (value != null) { 
      return value.toInstant().atZone(ZoneOffset.systemDefault()).toLocalDateTime(); 
     } 

     return null; 
    } 

    @Override 
    public Date convertToPresentation(LocalDateTime value, Class<? extends Date> targetType, Locale locale) 
      throws com.vaadin.data.util.converter.Converter.ConversionException { 

     if (value != null) { 
      return Date.from(value.atZone(ZoneOffset.systemDefault()).toInstant()); 
     } 

     return null; 
    } 

    @Override 
    public Class<LocalDateTime> getModelType() { 
     return LocalDateTime.class; 
    } 

    @Override 
    public Class<Date> getPresentationType() { 
     return Date.class; 
    } 

} 

Inspiriert von this link, die zwischen LocalDate und Date umwandelt. Der Konverter muss auf DateField über setConverter oder alternativ über converter factory eingestellt werden.

+0

FYI: Der Konverter ist jetzt Teil der Sammelmappe in Vaadin 8 und seine Schnittstelle hat sich geändert. Vaadin 8 kommt jetzt mit Konvertern für 'LocalDate' und' LocalDateTime', die Datumsfelder selbst basieren jetzt auf den neuen Datumstypen. –