Library code snippets

Calculate the difference between two dates, ignoring weekends

Description

The attached Java file contains three methods: the div() method provides integer division with rounding towards negative infinity, which Java does not provide by itself (the normal division and modulus operations round towards zero). Use of this function makes the other methods usable with dates before 1970 because it will handle negative numbers correctly.

The wdnum() method returns the number of weekdays (excluding weekends) that have passed since Monday, 29 December 1969. It works by calculating the number of days since 1 January, 1970 (getTime() divided by the number of milliseconds in a day), adding 3 and returning the number of week days in full weeks and possibly a partial week that have passed since then.

The diff() method shows one possible use of wdnum(): it calculates the number of weekdays that have passed between two given dates, excluding weekends.

An example use of the diff() function would be to calculate the number of working days in a month. This could be done as follows:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.set(year, month, 1);
Date d1 = cal.getTime();
cal.set(year, month + 1, 1);
Date d2 = cal.getTime();
System.out.println("Month " + month + ", " + year + " has " + diff(d1, d2) + " working days");

Note that the Calendar has to be set to the UTC time zone because the Date objects passed to diff() are expected to be relative to UTC.

Comments

  1. 21 Jul 2009 at 03:19
  2. 18 May 2009 at 16:14

    There are easier metyhods for this - one is to use Julian Calendar in relation. Day 1, 2,3,4,5...365

  3. 28 Jul 2007 at 16:41
    Hi srlanka,

    the code seems to have disappeared some time this year. I have reported to DF and hope access will be restored. In the mean time, you can see the code here: http://www.developerfusion.co.uk/forums/thread/137367/.

    Cheers,

    joki
  4. 06 Jul 2007 at 07:35
    Hi Joki
     I am also not able to see the Coding of your methods div(),wdnum() and diff(). Please provide the codings of your methods, then only we can utilize yours methods in our application, and we can able to post commets about your methods.

    Hari K......




  5. 05 Jul 2007 at 20:13
    I also do not see any code... I'm using firefox, anyone had succes with a different browser?

  6. 24 May 2007 at 17:07

    Where is the code? I don't get anything when I try to download Demo.java

     

    Thanks

  7. 01 Jan 1999 at 00:00

Leave a comment

Sign in or Join us (it's free).

 joki

Related podcasts

  • Java Posse #213 - Newscast for Oct 23rd 2008

    Newscast for Oct 23rd 2008 Fully formatted shownotes can always be found at http://javaposse.com The Android project has been released as open source, beating the rumored launch date for the source code by several months http://source.android.com/ And, Gizmodo and ZDNet both offer in-depth ...

Events coming up

  • Dec 15

    Portland Java User Group

    Portland, United States

    This month's topic: TBD----------PJUG meetings start with eat+meet+greet time (pizza and beverages are provided), followed by the featured speaker, then some time for Q&A, discussion, and sometimes a drawing to give away swag. :)It is...

Want to stay in touch with what's going on? Follow us on twitter!