Is there an easy way to get to human centric style periods in Joda Time?
For instance, if I wanted the last 4 days and by that I mean, give me a period (or interval might be more apt) 3 full days and the current in progress day up to the current time. So I would expect, an interval of 1 day, 1 day, 1 day, n number of ms up to the current time.
Or for the last 5 weeks, give me 4 full weeks and the current in progress week up to the current time.
I've spent some time with this and the difficult part is try to "normalize" a period to the human definitions of the end of the period. So for day to date you need to normalize to the start of the day (which is always midnight) and then find the interval between that and now. For Months, which are crazy tricky, you would need to normalize to the start of the month and go to now... etc. To make this even more difficult I'm trying to avoid writing a giant case statement with Period specific normalization logic and I am racking my brain trying find a generic approach.
Here is my current code (in Groovy)
static List generatePreviousIntervals(BaseSingleFieldPeriod period,
int count,
DateTime end = DateTime.now()) {
if (count < 1) {
return []
}
def intervals = []
Interval last
count.times {
def finish = last?.start ?: end
def next = new Interval(period, finish)
intervals.add(next)
last = next
}
// reverse so intervals are in normal progression order
intervals.reverse()
}
Is that something Joda Time can do easily? Or do I have to have a lot of logic around hand rolling specific intervals of time?