This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. General Coding and Design => Calculator Programming
Author Message
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 06 Nov 2007 12:43:26 pm    Post subject:

So I have two date objects and I want to find out how many days past one date is from the other. To be more specific they are both GregorianCalendar objects.
I tried doing

Code:
(date1.get(Calendar.DATE) - date2.get(Calendar.DATE))
but this only works if the dates don't wrap between months or years. I thought that the GregorianCalendar class would be smart enough to compensate for months, but not so. Is there some obvious thing that I am missing or is my best bet to go with something like...

Code:
int i;
while (!date2.equals(date1)) {
    i++;
    date2.add(Calendar.DATE, 1);
}


I haven't tried that final code, but i should hold the number of days between them b/c the add method wraps between months and years.
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 06 Nov 2007 01:35:24 pm    Post subject:

Do GregorianCalendar objects implement Comparable? If they do, try using the compareTo method to compare them. The amount returned should be something useful, like the number of days between them.
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 06 Nov 2007 02:05:53 pm    Post subject:

Using the value of compareTo, other than checking if it's less than, greater than, or equal to zero, seems to be in poor taste to me. Instead, you could, say, getTimeInMillis of both of the dates involved, divide by the number of milliseconds in a day, and subtract. That would probably be the best option if there's not a method specifically devoted for finding the number of days between two dates.
Back to top
magicdanw
pcGuru()


Calc Guru


Joined: 14 Feb 2007
Posts: 1110

Posted: 06 Nov 2007 02:31:25 pm    Post subject:

I didn't know any of the methods of GregorianCalendar objects, which is why I suggested the compareTo method, since it's so commonly implemented. I assumed that bananaman would have tried using a more exact date-retrieval method than get(Calendar.DATE) if one existed.
Back to top
bananaman
Indestructible


Calc Guru


Joined: 12 Sep 2005
Posts: 1124

Posted: 06 Nov 2007 03:50:35 pm    Post subject:

Originally I attempted to use the compareTo() method, but even after dividing by the amount of miliseconds in a day it came up with unpredictable results. I think that I will try the getTimeInMillis and see if that works.

It works great, thanks for the help.


Last edited by Guest on 06 Nov 2007 03:53:39 pm; edited 1 time in total
Back to top
elfprince13
Retired


Super Elite (Last Title)


Joined: 11 Apr 2005
Posts: 3500

Posted: 07 Nov 2007 05:15:29 pm    Post subject:

http://www.google.com/search?q=java+days+b...ate&btnG=Search

:lol:
Back to top
DarkerLine
ceci n'est pas une |


Super Elite (Last Title)


Joined: 04 Nov 2003
Posts: 8328

Posted: 07 Nov 2007 05:37:33 pm    Post subject:

I'd like to mention that you should most likely divide by the number of seconds or milliseconds in a day (whichever) before subtracting. Otherwise 6 PM on April 26th and 9 AM on April 27th will be 0 days apart, because the difference between them is less than 24 hours. But if you divide first, it will round down to the nearest day, and it will return the correct result of 1 day.

Last edited by Guest on 07 Nov 2007 05:37:45 pm; edited 1 time in total
Back to top
AlienCC
Creative Receptacle!


Know-It-All


Joined: 24 May 2003
Posts: 1927

Posted: 08 Nov 2007 05:35:40 am    Post subject:

Hmm, this topic isn't what I thought it was based on the title.
Back to top
alexrudd
pm me if you read this


Bandwidth Hog


Joined: 06 Oct 2004
Posts: 2335

Posted: 08 Nov 2007 12:58:39 pm    Post subject:

You forgot, perhaps, that this is a calculator/programming forum. Razz
Back to top
JoostinOnline


Active Member


Joined: 22 Aug 2007
Posts: 559

Posted: 08 Nov 2007 05:35:55 pm    Post subject:

When I first saw it I thought UTI was turning into some stupid dating site, but I am glad to see it remains a site for geeks! Very Happy
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement