亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Java 中,如何將 LocalDate 和時間格式的字符串連接到 LocalDateTime

在 Java 中,如何將 LocalDate 和時間格式的字符串連接到 LocalDateTime

喵喵時光機 2024-01-28 17:27:17
DatePicker我的嘗試是將 a (處理日期選擇,但不處理時間)和 a TextField(處理時間)的值統一為LocalDateTime兩者連接的可觀察值。我已經在模型中為兩者設置了可觀察的屬性,但我在加入它們時遇到了困難。到目前為止,我設法進行了一些嘗試Bindings.createObjectBinding(),但似乎沒有取得太大成功。我想至少知道我是否走在正確的道路上,或者我應該采取不同的方式嗎?
查看完整描述

1 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

通過使用,您可以從 a和 aLocalDateTime#of(LocalDate,LocalTime)創建 a?。您現在需要的是一種獲取 a和 a實例的方法。幸運的是,該控件為您提供了它的值,因此我們已經完成了。接下來是找到一種從 a獲取 a 的方法。這可以通過使用 a和 a來實現,a 和a 知道如何將 a 轉換為 a?,反之亦然。此用例有一個內置的:?。LocalDateTimeLocalDateLocalTimeLocalDateLocalTimeDatePickerLocalDateLocalTimeTextFieldTextFormatterStringConverterStringLocalTimeStringConverterLocalTimeStringConverter

一旦我們擁有了DatePicker和 the,TextFormatter我們就需要創建一個綁定,該綁定LocalDateTime從這兩個值創建 a。由于DatePickerTextFormatter都有一個value屬性,該屬性分別保存 aLocalDate和 (在本例中LocalTime為 a ),因此使用 來創建綁定相對簡單Bindings#createObjectBinding(Callable,Observable...)。

DatePicker dp = new DatePicker();

// Have to associate the TextFormatter with a TextField

TextFormatter<LocalTime> tf = new TextFormatter<>(new LocalTimeStringConverter());


ObjectBinding<LocalDateTime> binding = Bindings.createObjectBinding(() -> {

? ? LocalDate ld = dp.getValue();

? ? LocalTime lt = tf.getValue();

? ? return ld == null || lt == null ? null : LocalDateTime.of(ld, lt);

}, dp.valueProperty(), tf.valueProperty());

這是一個完整的示例:


import javafx.application.Application;

import javafx.beans.binding.Bindings;

import javafx.beans.binding.ObjectBinding;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.DatePicker;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

import javafx.scene.control.TextFormatter;

import javafx.scene.layout.HBox;

import javafx.scene.layout.VBox;

import javafx.stage.Stage;

import javafx.util.converter.LocalTimeStringConverter;


import java.time.LocalDate;

import java.time.LocalDateTime;

import java.time.LocalTime;

import java.time.format.DateTimeFormatter;


public class App extends Application {


? @Override

? public void start(Stage primaryStage) {

? ? DatePicker datePicker = new DatePicker();

? ? datePicker.setValue(LocalDate.now());


? ? TextField timeField = new TextField();

? ? TextFormatter<LocalTime> timeFieldFormatter =

? ? ? ? new TextFormatter<>(new LocalTimeStringConverter());

? ? timeField.setTextFormatter(timeFieldFormatter);

? ? timeFieldFormatter.setValue(LocalTime.now());


? ? HBox dateTimeBox = new HBox(10, datePicker, timeField);

? ? dateTimeBox.setAlignment(Pos.CENTER);


? ? ObjectBinding<LocalDateTime> ldtBinding = Bindings.createObjectBinding(() -> {

? ? ? LocalDate date = datePicker.getValue();

? ? ? LocalTime time = timeFieldFormatter.getValue();

? ? ? return date == null || time == null ? null : LocalDateTime.of(date, time);

? ? }, datePicker.valueProperty(), timeFieldFormatter.valueProperty());


? ? Label ldtLabel = new Label();

? ? ldtLabel.textProperty().bind(Bindings.createStringBinding(() -> {

? ? ? LocalDateTime dateTime = ldtBinding.get();

? ? ? return dateTime == null ? null : dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);

? ? }, ldtBinding));


? ? VBox root = new VBox(15, dateTimeBox, ldtLabel);

? ? root.setAlignment(Pos.CENTER);

? ? root.setPadding(new Insets(25));


? ? primaryStage.setScene(new Scene(root));

? ? primaryStage.show();

? }

}

上面將 a 的文本綁定Label到ObjectBinding<LocalDateTime>. TextFormatter只要文本被“提交”(例如,在獲得焦點Enter時按下) , 的值就會更新TextField。


我構建的方式LocalTimeStringConverter意味著它將使用我的Locale和FormatStyle.SHORT來解析和格式化LocalTime. 舉個例子,對我來說這意味著類似3:30 PMor 的東西11:25 AM。


查看完整回答
反對 回復 2024-01-28
  • 1 回答
  • 0 關注
  • 231 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號