• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

subtracting two dates

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i subtract two dates.I want number of days between them.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can check this thread:

http://www.java-forums.org/new-java/40-date-math.html
 
rajareddy annavaarm
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need number of working days between two dates(excluding saturday and sunday)

How can I do this?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Michael Dunn

I didnt understand the logic you have given.Kindly provide some sample code with some explanation.
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Not so fast naughty spawn! I want you to know about
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic