第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

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

1 回答

?
互換的青春

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個贊

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

一旦我們擁有了DatePicker和 the,TextFormatter我們就需要創(chuàng)建一個綁定,該綁定LocalDateTime從這兩個值創(chuàng)建 a。由于DatePickerTextFormatter都有一個value屬性,該屬性分別保存 aLocalDate和 (在本例中LocalTime為 a ),因此使用 來創(chuàng)建綁定相對簡單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只要文本被“提交”(例如,在獲得焦點(diǎn)Enter時按下) , 的值就會更新TextField。


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


查看完整回答
反對 回復(fù) 2024-01-28
  • 1 回答
  • 0 關(guān)注
  • 216 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號