× 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.



Which is why I shouldn't ever write code samples :) I tested the first
half of that case: starting at 00:00 and ending at 23:59, but should have
also test the reverse :(

I *think* this solves that problem. Assuming you don't actually need to
keep track of the hour and minutes fields after the calculation:

private static final long MILLISECONDS_IN_A_DAY = 1000 * 60 * 60 * 24;

...

date1 = new GregorianCalendar(2008, 01, 23, 20, 00); // WED 8PM
date2 = new GregorianCalendar(2008, 01, 25, 8, 00); // FRI 8AM

date1.clear(Calendar.HOUR_OF_DAY);
date2.clear(Calendar.HOUR_OF_DAY);

date1.clear(Calendar.MINUTE);
date2.clear(Calendar.MINUTE);

diff = Math.abs(date2.getTimeInMillis() - date1.getTimeInMillis());
diff = diff / (MILLISECONDS_IN_A_DAY);

System.out.println(diff);

Time to get back to work.

Don Yantzi
Technical Lead
WebSphere Development Studio Client for iSeries
IBM Toronto Lab





"Joe Pluta" <joepluta@xxxxxxxxxxxxxxxxx>
Sent by: java400-l-bounces@xxxxxxxxxxxx
01/27/2008 02:15 PM
Please respond to
Java Programming on and around the iSeries / AS400
<java400-l@xxxxxxxxxxxx>


To
"'Java Programming on and around the iSeries / AS400'"
<java400-l@xxxxxxxxxxxx>
cc

Subject
RE: Duration Calculation - slightly improved






From: Don Yantzi

Improved in that it's 4 less lines of code :)

GregorianCalendar date1 = new GregorianCalendar(2008, 01, 15, 0, 0);
GregorianCalendar date2 = new GregorianCalendar(2008, 01, 21, 23, 59);

long diff = Math.abs(date2.getTimeInMillis() - date1.getTimeInMillis());
diff = diff / (1000 * 60 * 60 * 24);

System.out.println(diff);

And the concern here is that the same number of milliseconds might
actually
represent different numbers of "days", depending on what you're trying to
determine.

For example, the number of seconds between 8AM on Tuesday and 8PM on
Wednesday is exactly the same as the number of seconds from 8PM on
Wednesday
to 8AM on Friday. However, depending on how your scheduling algorithm
works, I could easily envision a situation where the former is treated as
one day, while the latter must be treated as two.

Joe


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.