Reputation: 111
I am making an appointment app and am trying to get only the time of the appointment to fill into a combobox. So far I was able to get just the date, but now I am having trouble with receiving just the time. It shows up as yyyy-MM-dd HH:mm in the combobox. The code in question are the last two statements. The modifyDate successfully prints only the date, but I just can't seem to figure out how to separate the date off and print just the time. I tried to create another DateTimeFormatter with HH:mm but that did not work. Thank you so much!
This setAppointment is what puts the data from an existing appointment into the modify screen:
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));
Upvotes: 2
Views: 2053
Reputation: 60026
Yes you need to convert String
to LocalDateTime
and then extract the time part using another formater:
...
DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("HH:mm");
String time = LocalDateTime.parse(newAppointment.getStart(), format)
.format(timeFormat);
Or much better:
LocalTime localTime = LocalDateTime.parse(newAppointment.getStart(), format)
.toLocalTime();
Output
11:00
Upvotes: 3