【发布时间】:2020-07-21 07:11:27
【问题描述】:
我正在制作约会应用程序,并试图仅将约会时间填入组合框。到目前为止,我只能获得日期,但现在我无法获得时间。它在组合框中显示为 yyyy-MM-dd HH:mm。有问题的代码是最后两个语句。 modifyDate 仅成功打印日期,但我似乎无法弄清楚如何将日期分开并仅打印时间。我试图用 HH:mm 创建另一个 DateTimeFormatter ,但这没有用。非常感谢!
这个 setAppointment 将现有约会中的数据放入修改屏幕:
public void setAppointment(Appointment appointment, int index) {
selectedAppointment = appointment;
selectedIndex = index;
Appointment newAppointment = (Appointment) appointment;
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
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));
this.modifyStartComboBox.getSelectionModel().select(newAppointment.getStart(), format));
this.modifyEndComboBox.getSelectionModel().select(newAppointment.getEnd(), format));
【问题讨论】:
-
newAppointment.getStart()返回什么? -
它将从数据库中返回一个日期+时间。类似于“2020-03-27 11:00”,getEnd 将返回相同的日期,但结束时间。
-
您不应该在
Appointment类中将开始和结束时间保留为String。保留正确的日期时间对象,例如ZonedDateTime、Instant或LocalDateTime(取决于具体要求)。
标签: java date java-8 java-time