× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



This is part of the current problem - the date related classes seem
to be trying to deal with Daylight Savings Time or something and when
your dates cross the changeover point the results are inaccurate.

Besides - doesn't it strike you as thoroughly silly that one should
even have to contemplate "playing" with milliseconds to get a count
of a number of days! It is insane that such steps should be required
in a so-called "modern" language.

IHMO one of the problems is the name of the class! java.util.Date should
be called something like java.util.PointInTime since it represents a
specific moment, not a complete 24 hour day - and this would hopefully stop
so many programmers from being initially confused by how to use it.
There's no actual "date" class which can store a date without caring what
time of day it is.

Here's the code I wrote for it, and been using this for a while and have
JUnit tests for it which test all the DST change scenarios, so I'm pretty
sure it works OK, at least in the way I use it. It relies on and (mostly)
validates that you only pass it midnight times, so it wouldn't work with
the example posted using 8am/8pm times.

Hope this helps,

Nigel Gay,
Computer Patent Annuities Limited.

/**
* Counts the number of days between the two dates, i.e. 2nd date -
1st date; the dates are assumed to have no time component
* @param firstDate The earlier date
* @param secondDate The later date
* @return The number of days between the two dates
* @throws IllegalArgumentException If the dates have a time
component
*/
public static int calcDaysBetweenDates (Date firstDate, Date
secondDate)
throws IllegalArgumentException
{
// It won't necessarily be an even number of 24 hours between
the two dates - there might be daylight savings involved, in which case
// there may be a 23 or 25 hour day in there somewhere -
however it should be an even number of hours between the two dates
long difference = secondDate.getTime () - firstDate.getTime ();

if (difference < 0)
throw new IllegalArgumentException
("calcDaysBetweenDates: Second date must be equal to or later than the
first date");

long millisecondsInAnHour = 1000 * 60 * 60;

if (difference % millisecondsInAnHour != 0)
throw new IllegalArgumentException
("calcDaysBetweenDates: Dates must not have a time component (1)");

difference = difference / millisecondsInAnHour;

// We should only stray +/- 1 hour from regular 24 hour days
long remainder = difference % 24;
if ((remainder != 0) && (remainder != 1) && (remainder != 23))
throw new IllegalArgumentException
("calcDaysBetweenDates: Dates must not have a time component (2)");

// Adjust number of hours to make it an even 24
if (remainder == 23)
remainder = -1;

difference = (difference - remainder) / 24;

return (int) difference;
}


********************************************************************************
The information in this message is confidential and may be legally
privileged. It is intended solely for the addressee; access to this
email by anyone else is unauthorised.

If you are not the intended recipient: (1) you are kindly requested
to return a copy of this message to the sender indicating that you
have received it in error, and to destroy the received copy; and (2)
any disclosure or distribution of this message, as well as any action
taken or omitted to be taken in reliance on its content, is prohibited
and may be unlawful.
********************************************************************************

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.