I have been trying to compare 2 datetimes (selectedDateTime must be after currentDateTime) and if it is not then it shows a dialog box but when they are checked in the if statement clause, if I have a date for example 03/05/2023 11:59PM and it is currently 11:53PM it pops up the dialog box saying the time has passed when it should not. How can I fix this?
saveButton.addActionListener(new ActionListener() {
/**
* @param e the event to be processed
*/
@Override
public void actionPerformed(ActionEvent e) {
Date date = dateChooser.getDate();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int hour = (int) hourSpinner.getValue();
int minute = (int) minuteSpinner.getValue();
int second = (int) secondSpinner.getValue();
String amPm = (String) amPmComboBox.getSelectedItem();
String message = messageTextField.getText();
hourSpinner.setValue(hour);
LocalTime triggerTime = LocalTime.of(hour, minute, second);
ZoneId zone = ZoneId.systemDefault();
LocalDateTime selectedDateTime = LocalDateTime.of(localDate, triggerTime);
LocalDateTime currentDateTime = LocalDateTime.now(zone);
System.out.println("Selected DateTime: " + selectedDateTime);
System.out.println("Current Date Time: " + currentDateTime);
if (selectedDateTime.isBefore(currentDateTime)) {
JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
return;
}
/* Create the new Alarm object with the entries supplied by the user */
Alarm newAlarm = new Alarm(triggerTime, localDate, hour, minute, second, amPm, message);
model.addAlarm(newAlarm);
/* TESTING PURPOSES ONLY: prints out the alarm */
System.out.println(newAlarm);
/* Close down the alarm dialog */
alarmDialog.dispose();
/* Dialog to notify the user the alarm has been set */
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
JOptionPane.showMessageDialog(frame, "Your alarm has been set for: " + triggerTime + "" + amPm + " " + localDate.format(dateFormatter), "Alarm Set", JOptionPane.INFORMATION_MESSAGE);
}
});```