簡體   English   中英

使用 DateTimeFormatter 的索引解析問題

[英]Index parsing issue using DateTimeFormatter

我正在創建一個從名稱數據庫中提取的約會應用程序。 我創建了一個屏幕,可讓您修改約會。 我希望它填充已經存在的要更改的約會。 所有其他字段都在工作,但對於 modifyDate(這是我的 DatePicker),它返回一個錯誤。

在修改約會屏幕中填充信息:

public void setAppointment(Appointment appointment, int index) {
selectedAppointment = appointment;
selectedIndex = index;

Appointment newAppointment = (Appointment) appointment;

DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS");

this.modifyContactNameText.setText(newAppointment.getContact());
this.modifyTitleText.setText((newAppointment.getTitle()));
this.modifyURLText.setText((newAppointment.getUrl()));
this.modifyTypeText.setText((newAppointment.getType()));
this.modifyDescriptionComboBox.setValue((newAppointment.getDescription()));
this.modifyLocationComboBox.setValue((newAppointment.getLocation()));
this.modifyDate.setValue(LocalDate.parse(newAppointment.getStart(), format)); <----issue line
// I originally tried it like this:
// this.modifyDate.setValue((newAppointment.getStart())); 
// but that returned the time and date, and I just need the date

修改約會處理程序

    @FXML
void modifyAppointmentHandler(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader();     
        loader.setLocation(getClass().getResource("/View_Controller/ModifyAppointment.fxml"));
        loader.load();
        ModifyAppointmentController controller = loader.getController();
        Appointment appointment=appointmentTableView.getSelectionModel().getSelectedItem();
     
        int index = appointmentTableView.getSelectionModel().getSelectedIndex();
        controller.setAppointment(appointment, index);  <----issue line
     
        stage=(Stage) modifyAppointmentButton.getScene().getWindow();
        Parent scene = loader.getRoot();
        stage.setScene(new Scene(scene));
        stage.show();
        
    }

 

錯誤:

Caused by: java.time.format.DateTimeParseException: Text '2020-07-15 13:00' could not be parsed at index 16
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at View_Controller.ModifyAppointmentController.setAppointment(ModifyAppointmentController.java:275)
at View_Controller.AppointmentScreenController.modifyAppointmentHandler(AppointmentScreenController.java:206)
... 58 more
    

外觀示例,我填寫了位置和目的地,但需要日期、開始、結束在此處輸入圖像描述

索引 16 指的是格式字符串,特別是輸入中不存在的最后一個冒號。

查看您的時間戳是否缺少秒數。 嘗試更改行DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS"); DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM