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.
Related articles
Related discussion
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
Java Script, File uploading on ftp server using java script code
by h_c_a_andersen (2 replies)
-
How to set value to the cloned object?
by fsloke (1 replies)
-
Create this kind of website
by maidentv (1 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
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...
Thanks
!--removed tag-->There are easier metyhods for this - one is to use Julian Calendar in relation. Day 1, 2,3,4,5...365
!--removed tag-->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
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......
Where is the code? I don't get anything when I try to download Demo.java
Thanks
This thread is for discussions of Calculate the difference between two dates, ignoring weekends.