首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用变频器时在Vaadin DateField中设置范围

使用变频器时在Vaadin DateField中设置范围
EN

Stack Overflow用户
提问于 2016-11-24 15:55:14
回答 1查看 693关注 0票数 1

我正在使用一个带有转换器的Vaadin中的DateField来启用java.time包的LocalDateTime的使用。

当我使用转换器并通过setRangeEnd()限制DateField时,DateField总是显示一个带有消息'Date is out out of allowed Range‘的UserError。无需使用转换器,它就可以正常工作。

我的转换器:

代码语言:javascript
复制
public class LocalDateTimeToDateConverter implements Converter<Date,LocalDateTime> {

    private static final long serialVersionUID = -4900262260743116965L;

    @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()).toLocalDate().atStartOfDay();
        }

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

在其中使用DateField的MyView:

代码语言:javascript
复制
dateField = new DateField();
dateField.setDateFormat("yyyy-MM-dd");
dateField.setRangeStart(null);
dateField.setRangeEnd(Date.from(lastAvailableDataDate.atZone(ZoneId.systemDefault()).toInstant()));
dateField.setConverter(new LocalDateTimeToDateConverter());

谁知道我如何设置范围,同时使用转换器?

EN

回答 1

Stack Overflow用户

发布于 2016-11-24 18:49:32

我目前解决这个问题的方法是扩展DateField类。

代码语言:javascript
复制
public class MyDateField extends DateField {

private static final long serialVersionUID = -7056642919646970829L;

public MyDateField() {
    super();
}

public LocalDateTime getDate() {
    Date value = super.getValue();

    return DateToLocalDateTime(value);
}

public void setDate(LocalDateTime date) {
    super.setValue(LocalDateTimeToDate(date));
}

public void setRange(LocalDateTime start, LocalDateTime end) {

    if (start != null) {
        super.setRangeStart(LocalDateTimeToDate(start));
    } else {
        super.setRangeStart(null);
    }

    if (end != null) {
        super.setRangeEnd(LocalDateTimeToDate(end));
    } else {
        super.setRangeEnd(null);
    }
}

private Date LocalDateTimeToDate(LocalDateTime localDateTime) {
    return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}

private LocalDateTime DateToLocalDateTime(Date date) {
    return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
}

现在不再需要转换器,get/setDate()方法与我们的swing datepicker方法相对应。对于我们的情况,这可能是最好的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40780755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档