7 Replies Latest reply: Oct 7, 2009 6:09 PM by 796365

# Comparing 2 dates and putting results in predetermined buckets

Hi all

I want to write a method which takes in as parameters 2 date objects. It works out the difference (number of years) between the 2 dates and depending on this the results are flagged against 1 of 5 buckets.

Each bucket represents the following number of years 2, 5, 10, 20 and 30. So if my first 2 dates are taken and the number of years is 23 then the date should be stored against the 20 year bucket, or if the number of years is 6 then the date should be stored against the 5 year bucket, a value of 16 would be stored in the 20 year bucket etc...

I was looking for suggestions on the neatest way of doing this - any suggestions would be appreciated.
• ###### 1. Re: Comparing 2 dates and putting results in predetermined buckets
What are you actually storing in these buckets? ints? And what are these "buckets"? Lists?

Edited by: cotton.m on 7-Oct-2009 5:51 PM
• ###### 2. Re: Comparing 2 dates and putting results in predetermined buckets
Sorry my original post wasnt to clear.

When 2 dates are compared and the result is given e.g. 22 years - I need to add the second of the 2 dates to a container of some sort which represents 20 year things. So if the 2 dates are 01-Jan-2000 and 01-Jan-2022 then I would work out the difference in years which is 22 in this case, then the second date would be allocated to a bucket (an object which I havent decided on yet - e.g. could be a List or anything else) which hold dates which are closest to 20 years.

As in this example the difference is 22 years (not exactly 20) the logic would allocate the date to the 20 year bucket as 22 is closer to any of the other buckets:
2, 5, 10, 20 and 30. So I guess part of the logic is getting the difference in years e.g. 22 and then working out which of the 5 buckets this falls into.

I hope that makes it clearer - thanks again.
• ###### 3. Re: Comparing 2 dates and putting results in predetermined buckets
In general, subtract the value from each bucket value, get the absolute value of the result, and choose the bucket that that's closest. The exact details might be different depending on what the buckets are.
• ###### 4. Re: Comparing 2 dates and putting results in predetermined buckets
So I guess part of the logic is getting the difference in years e.g. 22 and then working out which of the 5 buckets this falls into.
Yup. Your first step is to decide which bucket to put a particular result into. But it sounds like you don't quite know what the requirements are yet, or at least you can't state them clearly. That should be the first thing you should do.
• ###### 5. Re: Comparing 2 dates and putting results in predetermined buckets
Hi I definitely know that I need to add e.g. 22 years to the 20 year bucket as opposed to the 30 year bucket and that a 26 year value would be allocated to the 30 year bucket as opposed to the 20 year one.

So the rules are definitely decide on its just coming up with the best way of doing this.

Edited by: notforgoogle on Oct 7, 2009 3:32 PM
• ###### 6. Re: Comparing 2 dates and putting results in predetermined buckets
Okay. So it's like this:

(1) Find the number of years apart.

(2) Figure out which bucket that thing goes into.

(3) Put the thing into the bucket.

So you say you have (2) figured out. What's your question, then?