3

Vaadin docsDateFieldはwithの使用方法を示していますが、 withを Java 8 type の Bean プロパティにバインドしjava.util.Dateたいと考えています。どうすればそれを達成できますか?DateFieldBeanFieldGroupjava.time.LocalDateTime

4

2 に答える 2

4

Vaadin コンバーターが進むべき道のようです:

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

}

と の間で変換するこのリンクに触発されました。コンバーターは、ビアまたはコンバーター ファクトリ経由で設定する必要があります。LocalDateDateDateFieldsetConverter

于 2016-05-15T09:32:18.667 に答える