Re: Workdays between dates? (OH HOW I HATE DATES)
Re: Workdays between dates? (OH HOW I HATE DATES)
- Subject: Re: Workdays between dates? (OH HOW I HATE DATES)
- From: Mark Morris <email@hidden>
- Date: Wed, 5 Mar 2008 10:04:53 -0600
Hi James,
Just looking at your list, you're getting what I would expect, i.e.
it's 4 days from Monday to Friday. But I think if you want the
calculation to be inclusive, it looks like you would always be safe in
adding one day to the end date before performing the calculation.
Regards,
Mark
On Mar 5, 2008, at 9:50 AM, James Cicenia wrote:
Ok -
Here are the results of my test (noticed the ***** inconsistencies):
What I am trying to get:
June 1 - June 7 (Sunday - Saturday) 5 Work Days.
June 2 - June 6 (Monday - Friday) 5 Work Days
June 2 - June 9 (Monday - next Monday ) 6 Work Days
June 7 - June 9 (Saturday - Monday) 1 Work Day
June 6 - June 7 (Friday - Saturday) 1 Work Day
June 7 - June 8 (Saturday - Sunday) 0 Work Day
What I am getting:
June 1 - June 7 (Sunday - Saturday) 5 Work Days.
June 2 - June 6 (Monday - Friday) 4 Work Days *****
June 2 - June 9 (Monday - next Monday ) 5 Work Days *****
June 7 - June 9 (Saturday - Monday) 0 Work Day *****
June 6 - June 7 (Friday - Saturday) 1 Work Day
June 7 - June 8 (Saturday - Sunday) 0 Work Day
Is this a bug? Is this my understanding? Is this hours? Is this
Java? Is this Insanity?
Regards,
James Cicenia
On Mar 4, 2008, at 5:01 PM, Lachlan Deck wrote:
Hi there,
just a few suggestions...
On 05/03/2008, at 3:48 AM, Simon McLean wrote:
public static int workingDaysBetweenDates(NSTimestamp start,
NSTimestamp end) {
NSTimestamp day = DateUtilities.normalisedDate(start);
int workingDays = 0;
// this could be quite slow... suggested alternative below
for (int i = 0; i <
DateUtilities
.daysBetweenDates(DateUtilities.normalisedDate(start), DateUtilities
.normalisedDate(end)); i++) {
if (DateUtilities.dayOfWeek(day, false).equals("Mon") ||
DateUtilities.dayOfWeek(day, false).equals("Tue")
|| DateUtilities.dayOfWeek(day, false).equals("Wed")
|| DateUtilities.dayOfWeek(day, false).equals("Thur")
|| DateUtilities.dayOfWeek(day, false).equals("Fri")) {
workingDays++;
}
day = day.timestampByAddingGregorianUnits(0, 0, 1, 0, 0, 0);
}
return workingDays;
}
public static int workingDaysBetweenDates( NSTimestamp start,
NSTimestamp end ) throws NullPointerException
{
Calendar a = Calendar.getInstance();
a.setTimeInMillis( start.getTime() );
Calendar b = Calendar.getInstance();
b.setTimeInMillis( end.getTime() );
int daysBetweenDates = daysBetweenDates( start, end );
int startDayOfWeek = a.get( Calendar.DAY_OF_WEEK );
int endDayOfWeek = b.get( Calendar.DAY_OF_WEEK );
daysBetweenDates -= 7 - startDayOfWeek;
daysBetweenDates -= endDayOfWeek - 1;
daysBetweenDates = daysBetweenDates / 5 * 5;
daysBetweenDates += 5 - ( startDayOfWeek - 2 );
if ( endDayOfWeek == Calendar.SATURDAY )
daysBetweenDates += 5;
else if ( endDayOfWeek > Calendar.SUNDAY )
daysBetweenDates += endDayOfWeek - 2;
return daysBetweenDates;
}
public static int daysBetweenDates(NSTimestamp t1, NSTimestamp
t2) {
if (t1 == null || t2 == null) {
return 0;
}
GregorianCalendar gc1 = new GregorianCalendar();
gc1.setTime(DateUtilities.normalisedDateGMT(t1));
GregorianCalendar gc2 = new GregorianCalendar();
gc2.setTime(DateUtilities.normalisedDateGMT(t2));
// not sure the above dates needed normalising.
// also the below will give a negative result I think.
int diff = (int) ((gc1.getTime().getTime() -
gc2.getTime().getTime()) / (1000 * 60 * 60 * 24));
return diff >= 0 ? diff : -1 * diff;
}
public static NSTimestamp normalisedDate( NSTimestamp timestamp )
{
return normalisedDate( timestamp, TimeZone.getDefault() );
}
public static NSTimestamp normalisedDate( NSTimestamp timestamp,
TimeZone zone )
{
Calendar result = Calendar.getInstance( zone );
result.setTimeInMillis( timestamp.getTime() );
int year = result.get( Calendar.YEAR );
int month = result.get( Calendar.MONTH );
int date = result.get( Calendar.DATE );
result.clear();
result.set( year, month, date );
return new NSTimestamp( result.getTimeInMillis() );
}
public static NSTimestamp normalisedDateGMT( NSTimestamp timestamp )
{
return normalisedDate( timestamp, TimeZone.getTimeZone( "GMT" ) );
}
// localizable version
public static String dayOfWeek(NSTimestamp aDate, boolean
longName) {
Calendar gc = Calendar.getInstance();
gc.setTime( aDate );
Format fm = new SimpleDateFormat( longName ? "EEEE" : "EEE" );
return fm.format( gc.getTime() );
}
public static String dayOfWeekFromInteger(String aDay, boolean
longName) {
if (aDay == "1") {
return longName ? "Sunday" : "Sun";
}
// Noooo.. We don't compare strings like that! :-)
// use:
if ("1".equals(aDay))
}
// But this is probably more portable/localisable:
public static String dayOfWeek( int day, boolean longName )
{
Calendar cal = Calendar.getInstance();
cal.set( Calendar.DAY_OF_WEEK, day );
return dayOfWeek( new NSTimestamp( cal.getTimeInMillis() ),
longName );
}
On 4 Mar 2008, at 16:37, James Cicenia wrote:
Anyone happen to have a code snippet that will give the number of
workday between two dates they would like to share?
with regards,
--
Lachlan Deck
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden