gpt4 book ai didi

特定格式的JavaFX DatePicker getValue

转载 作者:搜寻专家 更新时间:2023-10-31 19:54:48 24 4
gpt4 key购买 nike

我目前正在使用 Scene Builder 制作 javafx 场景。我想以特定格式从日期选择器中获取值。只需使用 datePicker.getValue() 即可以 yyyy-mm-dd 形式返回日期值。我想要 MMM dd, yyyy 形式。有人可以帮我吗?

目前完整的datePickerController代码是这样的

package date.picker;

import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;

/**
* FXML Controller class
*
* @author Dushyant Patel
*/
public class DatePickerController implements Initializable {

@FXML
private TextField display;
@FXML
private DatePicker datePicker;
@FXML
private Button getDateBtn;
@FXML
private Button setDateBtn;

@FXML
private void getDateAction(ActionEvent event) {

LocalDate date = datePicker.getValue();
if (date != null) {
display.setText(date.toString());
} else {
display.setText("");
}
}

@FXML
private void datePickerAction(ActionEvent event) {
LocalDate date = datePicker.getValue();
if (date != null) {
display.setText(date.toString());
} else {
display.setText("");
}
}

@FXML
private void setDateAction(ActionEvent event) {
if (!display.getText().trim().equals("")) {
if (display.getText().length() != 10) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error Dialog");
alert.setHeaderText("Date Error");
alert.setContentText("Please type date in the correct date format!");

alert.showAndWait();
} else {
LocalDate date = LocalDate.parse(display.getText());
datePicker.setValue(date);
}
} else {
datePicker.setValue(null);
}

}

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
String pattern = "MMM dd, yyyy";
StringConverter converter = new StringConverter<LocalDate>() {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);

@Override
public String toString(LocalDate date) {
if (date != null) {
return dateFormatter.format(date);
} else {
return "";
}
}

@Override
public LocalDate fromString(String string) {
if (string != null && !string.isEmpty()) {
return LocalDate.parse(string, dateFormatter);
} else {
return null;
}
}
};

datePicker.setConverter(converter);
}

}

最佳答案

DataPicker在数据选择器中引用转换器

 datePicker.setConverter(new StringConverter<LocalDate>() {
String pattern = "yyyy-MM-dd";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);

{
datePicker.setPromptText(pattern.toLowerCase());
}

@Override public String toString(LocalDate date) {
if (date != null) {
return dateFormatter.format(date);
} else {
return "";
}
}

@Override public LocalDate fromString(String string) {
if (string != null && !string.isEmpty()) {
return LocalDate.parse(string, dateFormatter);
} else {
return null;
}
}
});

关于特定格式的JavaFX DatePicker getValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26831978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com