如何将VaadinDateField绑定到LocalDateTime



Vaadin文档展示了如何将DateFieldjava.util.Date一起使用,但我希望将具有BeanFieldGroupDateField绑定到Java 8类型java.time.LocalDateTime的bean属性。我怎样才能做到这一点?

看起来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;
    }
}

LocalDateDate之间转换链路的启发。转换器需要通过setConverter或可替换地通过转换器工厂设置为DateField

这将是一个从LocalDate到LocalDateTime:的Vaadin 8转换器

package de.company.project.portal.application.views.documents;
import com.vaadin.data.Converter;
import com.vaadin.data.Result;
import com.vaadin.data.ValueContext;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Objects;
/**
 * A converter that converts between <code>LocalDate</code> and
 * <code>LocalDateTime</code>.
 *
 * Created from Vaadin v.8.13.0 LocalDateTimeToDateConverter
 */
public class LocalDateToLocalDateTimeConverter
        implements Converter<LocalDate, LocalDateTime>
{
    private ZoneId zoneId;
    /**
     * Creates a new converter using the given time zone.
     *
     * @param zoneId
     *            the time zone to use, not <code>null</code>
     */
    public LocalDateToLocalDateTimeConverter(ZoneId zoneId) {
        this.zoneId = Objects.requireNonNull(zoneId,
                "Zone identifier cannot be null");
    }

    @Override
    /** @return LocalDateTime from LocalDate with atTime(0,0,0,0) */
    public Result<LocalDateTime> convertToModel(LocalDate localDate,
                                                ValueContext context)
    {
        if (localDate == null) {
            return Result.ok(null);
        }
        return Result.ok(LocalDateTime.from(localDate.atTime(0,0,0,0))); //(hrs, mins, sec, nano sec)
    }

    @Override
    public LocalDate convertToPresentation(LocalDateTime localDateTime,
                                           ValueContext context)
    {
        if (localDateTime == null) {
            return null;
        }
        return LocalDate.from(localDateTime.atZone(zoneId));
    }
}

DTO DocumentDto:在活页夹中的应用

public void bindCreationDate(DateField field) {
    forField(field.getField())
            .withConverter(new LocalDateToLocalDateTimeConverter(ZoneId.systemDefault()))
            .bind(DocumentDto::getCreationDate, DocumentDto::setCreationDate);
}

相关内容

  • 没有找到相关文章

最新更新