java - 如何将 ObjectProperty<Date> 转换为 ObjectProperty<LocalDate>

标签 java javafx javafx-8

我要绑定(bind)

dp_date_add.valueProperty().bindBi Direction(model.forDateProperty());

其中 forDateProperty() 是:

public ObjectProperty<Date> forDateProperty() {
        if(forDate == null){
            forDate = new SimpleObjectProperty<>();
        }
        return forDate;
    }

问题是 bindBiderectional 仅接受 LocalDate。我试过这个:

dp_date_add.valueProperty().bindBidirectional(model.forDateProperty().get().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());

但它不起作用,因为它转换为 LocalDate,而不是 Property LocalDate。

最佳答案

如果可以的话,解决此问题的简单方法是更改​​模型,使其使用 ObjectProperty<LocalDate> 。假设您不能这样做,您需要使用两个监听器:

dp_date_add.valueProperty().addListener((obs, oldDate, newDate) -> 
    model.forDateProperty().set(Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant())));

model.forDateProperty().addListener((obs, oldDate, newDate) -> 
    dp_date_add.setValue(model.forDateProperty().get().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()));

关于java - 如何将 ObjectProperty<Date> 转换为 ObjectProperty<LocalDate>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31682580/

相关文章:

java - def 关键字的 Groovy 开销

intellij-idea - JavaFX 源代码未显示

java - 从文本数据库检索图像并显示在按钮上

java - 如何解决我的 JavaFX TableView 中的这个视觉故障?

java - 如何在Volley String Request(Android Studio)中提供json参数

java - Spring Boot 在使用 uritemplating 时不加载 css

java - 贝壳游戏。如何使物体移动,特别是在特定曲线上但以随机顺序移动?

css - 如何在 javafx 中为一行设置样式颜色?

java - 在 javafx 中向文本字段添加特殊字符

javafx - 需要 Bindings.equals ObservableList