在LocalDate构造函数中将日期选择器值用作参数时出错

时间:2019-03-27 04:53:42

标签: java javafx datepicker

我正在使用DatePicker获取LocalDate,然后将其转换为String。 但是每次我尝试从DatePicker获取年,月和日,然后将它们用作LocalDate构造函数中的参数。程序崩溃了。如果我只是在LocalDate的构造函数中手动键入年,月和日,那么它在TableView中可以正常工作

@Override
public void initialize(URL url, ResourceBundle rb) 
{
    roomTypeChoice.setValue("Select");
    roomTypeChoice.setItems(roomTypeList);

    //setting the cell of each Column that propertyValueFactory should make sure that 
    //the variable name should be consisted with memeber variable name otherwise the tableview can not 
    //display the value
    firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
    lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
    checkInCol.setCellValueFactory(new PropertyValueFactory<>("checkInDate"));
    checkOutCol.setCellValueFactory(new PropertyValueFactory<>("checkOutDate"));      
    phoneCol.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
    addressCol.setCellValueFactory(new PropertyValueFactory<>("address"));
    emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));



}    

@FXML
private void checkIn(ActionEvent event)  
{

    //get the Check in date convert to Date Object from DatePicker
    LocalDate checkInLocalDate=LocalDate.of(checkInDatePicker.getValue().getYear(), checkInDatePicker.getValue().getMonthValue(), checkInDatePicker.getValue().getDayOfMonth());
    String checkInDate=checkInLocalDate.toString();
    System.out.println(checkInLocalDate.toString()+"");
    Customer customer=new Customer(firstNameTextField.getText().trim(),lastNameTextField.getText().trim(),checkInDate,checkInDate
    ,phoneTextField.getText().trim(),addressTextField.getText().trim(),emailTextField.getText().trim());


    tableView.getItems().add(customer);



}

我希望摆脱这个困扰我很多兄弟的问题。

1 个答案:

答案 0 :(得分:0)

尝试一下

LocalDate localDate = checkInDatePicker.getValue();

int day = localDate.getDayOfMonth();
int month = localDate.getMonthValue();
int year = localDate.getYear();