#Comparing 2 datetimes (if one is before the other)

1 messages · Page 1 of 1 (latest)

sinful smelt
#

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);
            }


        });```
hasty magnetBOT
# sinful smelt I have been trying to compare 2 datetimes (selectedDateTime must be after curren...

Detected code, here are some useful tools:

Formatted code
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);
}
}
);
#

<@&987246399047479336> please have a look, thanks.

hasty magnetBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

sinful smelt
#

What is printed out from my print statements:

Selected DateTime: 2023-05-04T12:00
Current Date Time: 2023-05-04T00:56:22.671600200
Date: 04-05-2023, Time: 12:00AM, Message:

#

I think I fixed it, let me check

#

Doing this:


   if (selectedDateTime.compareTo(currentDateTime) < 0) {
                    System.out.println("Selected Date occurs before current date");
                } else if (selectedDateTime.compareTo(currentDateTime) > 0) {
                    System.out.println("Current date occurs before selected date");
                } else {
                    System.out.println("Both dates are equal");
                }

hasty magnetBOT
sinful smelt
#

Never mind, it did not

#

Currently have this now:


  /* Add a property change listener to the saveButton component */
        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);




                /* Selected Date occurs before Current Date */
                if (selectedDateTime.compareTo(currentDateTime) < 0) {
                    System.out.println("Selected Date occurs before Current Date");
                    JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
                    return;

                    /* Current Date occurs before Selected Date */
                } else if (selectedDateTime.compareTo(currentDateTime) > 0) {
                    System.out.println("Current Date occurs before Selected Date");
                }


                System.out.println("Selected DateTime: " + selectedDateTime);
                System.out.println("Current Date Time: " + currentDateTime);


                /* 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);
            }


        });```
hasty magnetBOT
# sinful smelt Currently have this now: ```java /* Add a property change listener to the sa...

Detected code, here are some useful tools:

Formatted code
/* Add a property change listener to the saveButton component */
 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);
 
 
 
 
 /* Selected Date occurs before Current Date */
 if (selectedDateTime.compareTo(currentDateTime) < 0) {
 System.out.println("Selected Date occurs before Current Date");
 JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
 return;
 
 /* Current Date occurs before Selected Date */
 } else if (selectedDateTime.compareTo(currentDateTime) > 0) {
 System.out.println("Current Date occurs before Selected Date");
 }
 
 
 System.out.println("Selected DateTime: " + selectedDateTime);
 System.out.println("Current Date Time: " + currentDateTime);
 
 
 /* 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);
}
}
);
sinful smelt
#

It is 01:26AM and it for some reason allows me to select 12:00AM today which has passed

#

Won't let me select 1:00PM

#

today

#

dates are muddled up in terms of AM/PM it seems

#

how do I fix this?

#

xD

#

Ok its not

#

wtf

#
 /* Selected Date occurs before Current Date */
                if (selectedDateTime.compareTo(currentDateTime) < 0) {
                    System.out.println("Selected Date occurs before Current Date");
                    JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
                    return;

                    /* Current Date occurs before Selected Date */
                } else if (selectedDateTime.compareTo(currentDateTime) > 0) {
                    System.out.println("Current Date occurs before Selected Date");
                }


                System.out.println("Selected DateTime: " + selectedDateTime);
                System.out.println("Current Date Time: " + currentDateTime);```
hasty magnetBOT
# sinful smelt ```java /* Selected Date occurs before Current Date */ if (sele...

Detected code, here are some useful tools:

Formatted code
/* Selected Date occurs before Current Date */
 if (selectedDateTime.compareTo(currentDateTime) < 0) {
 System.out.println("Selected Date occurs before Current Date");
 JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
 return;
 
 /* Current Date occurs before Selected Date */
}
else if (selectedDateTime.compareTo(currentDateTime) > 0) {
System.out.println("Current Date occurs before Selected Date");
}
System.out.println("Selected DateTime: " + selectedDateTime);
System.out.println("Current Date Time: " + currentDateTime);
sinful smelt
#

Current Date occurs before Selected Date
Selected DateTime: 2023-05-04T12:00
Current Date Time: 2023-05-04T01:30:03.318586700
Date: 04-05-2023, Time: 12:00AM, Message:

#

It is not comparing on time, but number size it seems?

sinful smelt
#

@slate gate can you point me in the right direction plz?

slate gate
sinful smelt
slate gate
sinful smelt
#

Sort of but say it’s 1:00Am today, it lets me select 12:00Am which has passed..

#

It’s not working how it should

sinful smelt
slate gate
sinful smelt
#

But it’s 1;30 am and that selected was 12:00am which had passed?

sinful smelt
#

Maybe I need to switch from 12 hour to 24 hour? 🤔

slate gate
#

Well yes it is

#

The previous if is wrong

#

< 0

sinful smelt
#

this is wrong? bro

#

I updated it, this should be correct, let me see in 5 mins.

#

It lets me set an alarm for today at 1AM (which has already passed) bro

#

@slate gate

#

when I fix it

#

same for 12AM

#

selectedTime 2023-05-04T08:00
currentDateTime 2023-05-04T19:19:18.694937500
Selected Date occurs on or before Current Date/Time.

#

when I try set an alarm for today 8:00PM

#

but its only 7:19PM for me..

sinful smelt
#

@little basin @slate gate @wet mirage please can someone help me?

#

I need someone who is experienced with this in Java

#
            /**
             * @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);


                LocalDateTime selectedDateTime = LocalDateTime.of(localDate, triggerTime);
                LocalDateTime currentDateTime = LocalDateTime.now();


                System.out.println("selectedTime " + selectedDateTime);
                System.out.println("currentDateTime " + currentDateTime);

                /* Selected Date occurs after Current Date or Time */
                if (selectedDateTime.isAfter(currentDateTime)) {
                    System.out.println("Selected Date occurs on or before Current Date/Time.");
                    System.out.println("Alarm is successful!!!!!!!!!!!!!!!!!");
                    /* 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);

                    /* 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);
                }
                /* Current Date occurs before Selected Date */
                else if (!(selectedDateTime.compareTo(currentDateTime) > 0)) {
                    System.out.println("Current Date occurs before Selected Date");
                    JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed2.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
                    return;

                    /* Both dates are equal */
                } else if (selectedDateTime.compareTo(currentDateTime) == 0) {
                    System.out.println("Both times are equal.");
                    JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed3.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!WARNING_MESSAGE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("Selected DateTime: " + selectedDateTime);
                System.out.println("Current Date Time: " + currentDateTime);
                  

            }


        });```
hasty magnetBOT
# sinful smelt ```java saveButton.addActionListener(new ActionListener() { /** ...

Detected code, here are some useful tools:

Formatted code
java 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);
   
   
   LocalDateTime selectedDateTime = LocalDateTime.of(localDate, triggerTime);
   LocalDateTime currentDateTime = LocalDateTime.now();
   
   
   System.out.println("selectedTime " + selectedDateTime);
   System.out.println("currentDateTime " + currentDateTime);
   
   /* Selected Date occurs after Current Date or Time */
   if (selectedDateTime.isAfter(currentDateTime)) {
   System.out.println("Selected Date occurs on or before Current Date/Time.");
   System.out.println("Alarm is successful!!!!!!!!!!!!!!!!!");
   /* 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);
   
   /* 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);
   }
   /* Current Date occurs before Selected Date */
   else if (!(selectedDateTime.compareTo(currentDateTime) > 0)) {
   System.out.println("Current Date occurs before Selected Date");
   JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed2.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
   return;
   
   /* Both dates are equal */
}
else if (selectedDateTime.compareTo(currentDateTime) == 0) {
  System.out.println("Both times are equal.");
  JOptionPane.showMessageDialog(frame, "Please select a time and date that has not passed3.", "Invalid Time/Date", JOptionPane.WARNING_MESSAGE);
  return ;
}
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!WARNING_MESSAGE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("Selected DateTime: " + selectedDateTime);
System.out.println("Current Date Time: " + currentDateTime);
}
}
);
sinful smelt
#

This is what I have now, only lets me create alarms for next day.. not today

slate gate
sinful smelt
#

Fixed it

#

just removed AM/PM

#

was overcomplicated

#

made it 24 hr time