It's amusing how difficult this is, isn't it? If you do a Google search you will find a number of examples of solutions that don't work -- or rather, solutions that give answers that are technically correct but aren't what you wanted. The problem you are running into is daylight savings time and/or leap seconds. The GregorianCalendar will tell you the number of milliseconds between two dates but is rather vague about how many days that is. You see, some days are 23 hours long and others are 25 (daylight savings) and some days are 24 hours and one second long (leap seconds).
The Java Date/Time classes are exactly what you need to launch a probe to Uranus but are maddening to try to use for business calculations. One always feels like Captain Kirk trying to get Mr. Spock to answer a simple question.
If you are just interested in subtracting even-day dates you could try adding enough milliseconds to make, say, four hours before you divide by the number of milliseconds in a day (then truncate to an integer). Alternatively, you could look for a different calendar class that is more amenable to business calculations. My Google search found several but I haven't taken the time to find one I liked. I also have some classes that I wrote several years ago that implement a simplified time model where all days are 24 hours long, all hours have exactly 60 minutes, etc.. If you are interested I will see if I can find them. They are a couple of years old and may have some deprecated (old-style) calls to system classes but I would love to put them back in service. They were pretty handy.
Lee Haslup
At 10:10 AM 6/16/2003 -0400, you wrote:
I am new to Java and could use some advice, help, examples .. anything I can get ..
I am trying to calculate the number of days between two gregorian dates.
Example 04/10/1992 - 04/08/1992 = 2 days.
I am using the GregorianCalendar and it works fine as long as the dates fall within the same year. However, if one of the dates is of a different year ( e.g. - 04/10/1992 - 02/15/1991 ) the calculation does not work correctly ..
Does anyone out there have any ideas as to what I am doing wrong?
_______________________________________________ Juglist mailing list [EMAIL PROTECTED] http://trijug.org/mailman/listinfo/juglist_trijug.org
