在Java中,如何将LocalDate和时间格式的字符串连接到LocalDateTime的一个可观察属性中?
我试图将在Java中,如何将LocalDate和时间格式的字符串连接到LocalDateTime的一个可观察属性中?,java,javafx,binding,localtime,Java,Javafx,Binding,Localtime,我试图将DatePicker(处理日期选择,但不处理时间)和TextField(处理时间)的值统一到两者的可观察LocalDateTime 我已经在模型中为这两者设置了可观察属性,但是我很难将它们连接起来 到目前为止,我尝试了几次绑定。createObjectBinding(),但似乎没有太大成功 我想知道我是否走在正确的道路上,或者我是否应该以不同的方式进行此操作?通过使用,您可以从LocalDate和LocalTime创建LocalDateTime。您现在需要的是一种获取LocalDate和
DatePicker
(处理日期选择,但不处理时间)和TextField
(处理时间)的值统一到两者的可观察LocalDateTime
我已经在模型中为这两者设置了可观察属性,但是我很难将它们连接起来
到目前为止,我尝试了几次绑定。createObjectBinding()
,但似乎没有太大成功
我想知道我是否走在正确的道路上,或者我是否应该以不同的方式进行此操作?通过使用,您可以从LocalDate
和LocalTime
创建LocalDateTime
。您现在需要的是一种获取LocalDate
和LocalTime
实例的方法。幸运的是,DatePicker
控件将其值作为LocalDate
提供给您,因此我们就完成了。下一步是找到从TextField
获取LocalTime
的方法。这可以通过使用和来实现,它们知道如何将字符串
转换为本地时间
,反之亦然。这个用例有一个内置的StringConverter
:
一旦我们有了DatePicker
和TextFormatter
之后,我们需要创建一个绑定,该绑定从这两个值创建一个LocalDateTime
。由于DatePicker
和TextFormatter
都有一个value
属性,分别保存LocalDate
和LocalTime
,因此创建绑定相对简单
DatePicker dp=newdatepicker();
//必须将TextFormatter与TextField关联
TextFormatter tf=新的TextFormatter(新的LocalTimeStringConverter());
ObjectBinding=Bindings.createObjectBinding(()->{
LocalDate ld=dp.getValue();
LocalTime lt=tf.getValue();
返回ld==null | | lt==null?null:LocalDateTime.of(ld,lt);
},dp.valueProperty(),tf.valueProperty());
下面是一个完整的示例:
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.beans.binding.ObjectBinding;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.DatePicker;
导入javafx.scene.control.Label;
导入javafx.scene.control.TextField;
导入javafx.scene.control.TextFormatter;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入javafx.util.converter.LocalTimeStringConverter;
导入java.time.LocalDate;
导入java.time.LocalDateTime;
导入java.time.LocalTime;
导入java.time.format.DateTimeFormatter;
公共类应用程序扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
DatePicker DatePicker=新的日期选择器();
setValue(LocalDate.now());
TextField timeField=新的TextField();
文本格式化程序timeFieldFormatter=
新的TextFormatter(新的LocalTimeStringConverter());
setTextFormatter(timeFieldFormatter);
timeFieldFormatter.setValue(LocalTime.now());
HBox dateTimeBox=新的HBox(10,日期选择器,时间字段);
dateTimeBox.setAlignment(位置中心);
ObjectBinding ldtBinding=绑定。createObjectBinding(()->{
LocalDate=datePicker.getValue();
LocalTime time=timeFieldFormatter.getValue();
返回日期==null | |时间==null?null:LocalDateTime.of(日期,时间);
},datePicker.valueProperty(),TimeFinder Formatter.valueProperty();
Label ldtLabel=新标签();
ldtLabel.textProperty().bind(Bindings.createStringBinding(()->{
LocalDateTime dateTime=ldtBinding.get();
return dateTime==null?null:dateTime.format(DateTimeFormatter.ISO\u LOCAL\u DATE\u TIME);
},ldtBinding);
VBox root=新的VBox(15,日期时间框,ldtLabel);
根部设置对齐(位置中心);
根。设置填充(新插图(25));
primaryStage.setScene(新场景(根));
primaryStage.show();
}
}
上面将标签的文本绑定到对象绑定
。每当文本“提交”时,TextFormatter
的值就会更新(例如,在TextField
有焦点时按Enter键)
我构造LocalTimeStringConverter
的方式意味着它将使用我的Locale
和FormatStyle.SHORT
来解析和格式化LocalTime
。例如,对我来说,这意味着像下午3:30或上午11:25。这是可定制的。请参见LocalTimeStringConverter
的各种构造函数。您能给我们展示一下您尝试过的代码吗?我不完全明白你想要达到的目标。。。将日期选择器
(到目前为止类型未知)和字符串
的结果组合起来似乎是可能的,但我不知道可观察的部分。你是说组合成可观察的LocalDateTime吗?是的,这是正确的。我将进行编辑。@deHaar,我编辑了我的问题以提供数据类型。这有用吗?我的代码有点分散在几个类中,如果需要,我将尝试稍后创建一个最小可行的演示。dateStartInput.getValue()
不是LocalDate
,您必须首先像LocalDate.parse(dateStartInput.getValue(),DateTimeFormatter.ISO\u DATE)那样解析它。因为LocalDateTime.of(LocalDate,LocalTime)
。。。