I am wondering what is the best way, given a TreeSet of Date objects, to tell if these dates are daily or monthly. Note the set of daily dates will not have dates for weekends or bank holidays. The set of monthly dates will have month-end dates. The set of dates will span a few years.
The only way I can presently think of is to take the first 12 dates from the set and inspect each date’s month. If a month is found more than once in succession then this is likely to be a set of daily dates. If not then it can be assumed that it’s a set of monthly dates.
Whilst this seems like it should work (I can’t think of any edge-cases that it would miss) I was wondering if there were a more elegant solution? I would imagine some sort of data structure (a tree?) that uses a comparator based on month and year. Then if a sub-tree of this collection contained more than one node then I could assume they were daily dates. I’m not sure if this way would warrant the extra code or if there’s a better solution available. Thanks for any help you can give me.
That won’t necessarily work.
You potentially have to check each and every date in the set to determine if the date is a week day, and end-of-month day … or both!!
You could stop when you find a week-day that is not an end-of-month day, or vice versa, but a defensive program would check all of them, in case you’ve been given bad input.
You won’t get any performance benefit from the fact that the collection is a
TreeSet.