Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# subtracting two dates

rajareddy annavaarm
Ranch Hand

Joined: Mar 15, 2007
Posts: 96
How can i subtract two dates.I want number of days between them.
rohit leeta
Ranch Hand

Joined: May 02, 2007
Posts: 49

http://www.java-forums.org/new-java/40-date-math.html

<a href="http://www.java-forums.org" target="_blank" rel="nofollow">Java Forums</a>
rajareddy annavaarm
Ranch Hand

Joined: Mar 15, 2007
Posts: 96
I need number of working days between two dates(excluding saturday and sunday)

How can I do this?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a counter
Calendar object for startDate
Calendar object for endDate
a while loop:
1) checks if startDate DAY_OF_WEEK % 7 is > 1 (if true, counter++)
2) adds a day to startDate

for the while condition you use
startDate.before(endDate) or
startDate.after(endDate) == false
a few ways to do the while condition, depending on whether start/endDates
are inclusive/exclusive etc

at the end of the loop, counter will be the number of working days, but this
will also include public holidays - you'll have to determine how to handle this
rajareddy annavaarm
Ranch Hand

Joined: Mar 15, 2007
Posts: 96
hi Michael Dunn

I didnt understand the logic you have given.Kindly provide some sample code with some explanation.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 )
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 37611

18
Yes, I think looping day by day from start to end day is kind of a last resort. But many people have encountered this problem before -maybe not the twist about working days-, so there are plenty of example source codes Google knows about. If you want to account for holidays as well it'll get trickier, of course.

Ping & DNS, my free Android app for network diagnostics

I agree. Here's the link: http://aspose.com/file-tools

subject: subtracting two dates