posted 17 years ago
Isn't doing a loop going to be quite resource intensive? For example, suppose I wanted to find the number of working days between 1st May 200BC and 1st May 2200 - that'll be 730K iterations (each containing a couple of comparisons, modulo and increment operator). Of course, if we're only talking dates localised to the recent decades, then you'll probably be okay.
Are you sure there isn't a closed formula for this? It'll have to take into account leap years, but otherwise aren't the number of weekdays just 5 in 7 of the total number of days (you'll need to count carefully to make sure partial week counts don't upset the total, taking into account starting/ending on a weekend)? There are 365 days in a non-leap year, and a well-defined number of days in each month. Public holidays are more difficult because they vary depending on the locality, though if you really want to be sophisticated, you could use Locale.
Try to be careful with a mathematical argument and it's sure to be successful - and probably a two line piece of code in the end which executes with constant time (with the right form of input). Public holidays, if you really need to take them into account, will complicate the code but shouldn't make it too unbearable (e.g. same number every year, so you only need to work out the first and last years as a special case).
See what you can come up with...
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )