Re: Calculating the difference of days between two dates in Java
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Fri, 27 Jul 2007 15:23:55 -0400
Andy Jiang wrote On 07/27/07 09:17,:
Hello,everyone! I used the Date class to create two dates by invoking the
constructor Date(long date), but can anybody tell me how to calculate the
difference of days between these two dates? Which methods should I use?
It depends on what you mean by "difference of
days." For example, if the first Date represents
2007-07-26 23:59:59.999 and the second represents
2007-07-27 00:00:00.001, is the difference a tiny
fraction of a day (two milliseconds) or is it one
day (the Dates correspond to different calendar
days), or is it something else altogether? The way
you define the desired answer has a lot to do with
how you'll go about calculating it ...
You've already been given some answers for the
"two milliseconds" interpretation: subtract the two
time stamps and divide by the number of milliseconds
in a day. I'll just add that you don't need the Date
objects to do this: you are using the Date(long)
constructor, so you already possess the millisecond
time stamps that you'll want to subtract.
For the "one day" interpretation, you will probably
want to use GregorianCalendar instead of Date. I'd
suggest using subtract-and-divide to get an initial
estimate, and then making adjustments if needed.
Untested:
static int daysBetween(long time1, long time2) {
// first approximation:
int days = (int)(
(time2 - time1) / (24 * 3600 * 1000) );
// get year and day for time1:
Calendar cal = new GregorianCalendar();
// optional: use different time zone
cal.setTimeInMillis(time1);
int year1 = cal.get(Calendar.YEAR);
int yday1 = cal.get(Calendar.DAY_OF_YEAR);
// go to time2, then step backward:
cal.setTimeInMillis(time2);
cal.add(Calendar.DAY_OF_YEAR, -days);
// correct for over- or under-shooting:
int y = cal.get(Calendar.YEAR);
int d = cal.get(Calendar.DAY_OF_YEAR);
while (y != year1 || d != yday1) {
int adj = (y < year1
|| (y == year1 && d < yday1))
? +1 : -1;
cal.add(Calendar.DAY_OF_YEAR, adj);
days -= adj;
y = cal.get(Calendar.YEAR);
d = cal.get(Calendar.DAY_OF_YEAR);
}
return days;
}
Notes: [1] The comment about stepping "backward"
assumes time1 <= time2; the step will actually be
forward if time1 > time2. [2] It's because time1
might be later than time2 that we must be able to
adjust in either direction. [3] The while loop may
look slow, but it usually won't execute at all and
will execute more than once only in extraordinary
circumstances (e.g., when time1 and time2 are on
opposite sides of the start of Gregorian reckoning;
maybe not even then).
--
Eric.Sosman@xxxxxxx
.
- Follow-Ups:
- Re: Calculating the difference of days between two dates in Java
- From: Eric Sosman
- Re: Calculating the difference of days between two dates in Java
- References:
- Calculating the difference of days between two dates in Java
- From: Andy Jiang
- Calculating the difference of days between two dates in Java
- Prev by Date: sending and receiving packets
- Next by Date: Re: sending and receiving packets
- Previous by thread: Re: Calculating the difference of days between two dates in Java
- Next by thread: Re: Calculating the difference of days between two dates in Java
- Index(es):
Relevant Pages
|