• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Workdays between dates?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Workdays between dates?


  • Subject: Re: Workdays between dates?
  • From: Lachlan Deck <email@hidden>
  • Date: Wed, 5 Mar 2008 10:01:06 +1100

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


  • Follow-Ups:
    • Re: Workdays between dates? (OH HOW I HATE DATES)
      • From: James Cicenia <email@hidden>
References: 
 >Workdays between dates? (From: James Cicenia <email@hidden>)
 >Re: Workdays between dates? (From: Simon McLean <email@hidden>)

  • Prev by Date: Re: Lifebeat
  • Next by Date: WOWebServiceClient & WO 5.4.1
  • Previous by thread: Re: Workdays between dates?
  • Next by thread: Re: Workdays between dates? (OH HOW I HATE DATES)
  • Index(es):
    • Date
    • Thread