如何将Vaadin DateField绑定到LocalDateTime

编程入门 行业动态 更新时间:2024-10-10 21:29:37
本文介绍了如何将Vaadin DateField绑定到LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Vaadin docs 显示了如何使用DateField与java.util.Date,但我想将DateField与BeanFieldGroup绑定到Java 8类型java.time.LocalDateTime的bean属性.我该如何实现?

The Vaadin docs show how to use the DateField with java.util.Date but I want to bind the DateField with a BeanFieldGroup to a bean property of Java 8 type java.time.LocalDateTime. How can I achieve that?

推荐答案

似乎像 Vaadin Converter 是必经之路:

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

灵感来自此链接,它将在LocalDate和Date之间转换.需要通过setConverter或通过转换器工厂.

Inspired by this link which converts between LocalDate and Date. The converter needs to be set to the DateField via setConverter or alternatively via a converter factory.

更多推荐

如何将Vaadin DateField绑定到LocalDateTime

本文发布于:2023-10-29 00:53:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538223.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   如何将   DateField   Vaadin   LocalDateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!