我要绑定(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/