Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Not sure why these dates arent comparing!  (Read 920 times)
0 Members and 1 Guest are viewing this topic.
Offline Swattkidd7

Junior Duke





« Posted 2008-06-03 00:45:24 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
   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.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2008-06-03 03:13:44 »

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.

See my work:
OTC Software
Offline Swattkidd7

Junior Duke





« Reply #2 - Posted 2008-06-03 04:11:53 »

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?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!