Not sure why these dates arent comparing!

This is really leaving me confused. Basically im trying to create this App that will let you know when the time you set has occurred or w.e… Like an alarm clock type thing.

Here is the code, the problem is that the APPOINTMENT dialog box is not popping up when the times are equal. It does pop up when i just test it and compare Dates that i know are the same.

Here is the code

   public void run() {
        
        // lower ThreadPriority
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        // run a long while (true) this means in our case "always"
        while(true)
        {
            appts = gui.getAppts();
            date = new Date();
            gui.updateDate(date);
            for(int i = 0; i < appts.size(); i++)
            {
                Appointment a = appts.get(i);
                Date ab = a.getDate();
                System.out.println(ab+"            "+date);
                if(ab.compareTo(date) == 0)
                {
                    JOptionPane.showConfirmDialog(null,"Appointment!");
                }
            }
            
            repaint();
            
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            
            // set ThreadPriority to maximum value
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }

Basically i get the Appointments ArrayList
set the date variable as the new (current) date
Update the GUI with the new date

Then i cycle through all the appointments and see if the dates compare to eachother and equal 0 if it does, then i show the DialogBox…

I even Printed out the dates to see when they are the same, and at one point they are the same, but it still does not show up…

THanks for the help in advanced.

Well Date has precision down to the millisecond, so chances are your program is skipping over that single millisecond when things are the same. Instead of using .equals() or compareTo(), you should write your own comparison method.

You can do this either with GregorianCalendar, or with Date.getTime(). GregorianCalendar can let you compare the date, hour, minute, and second (if you like), whereas getTime() will let you compare the actual milliseconds. That way you can keep it as precise as you want.

Ohh thanks i would have been stuck on that forever!!

But now I have a problem, I have an Appointment Object that holds a Date and a note.

And in the code above i added the GregorianCalendar object, and use getTime(). But the Appointment object still holds the normal Date object, how could i get the Appointment Object to hold a gregorian time so i could compare times?

WOuld i pass the gregorian calendar to the Appointment Object so they are all using the same time?