views:

140

answers:

2

I'm using the following snipped to find the begin and end of several time periods in Joda. The little devil on my left shoulder says thats the way to go... but I dont believe him.

Could anybody with some joda experience take a brief look and tell me that the little guy is right?

(It will be only used for UTC datetime objects)

Thank you!

/* Year */
private static DateTime endOfYear(DateTime dateTime) {
    return endOfDay(dateTime).withMonthOfYear(12).withDayOfMonth(31);
}

private static DateTime beginningOfYear(DateTime dateTime) {
    return beginningOfMonth(dateTime).withMonthOfYear(1);
}

/* Month */
private static DateTime endOfMonth(DateTime dateTime) {
    return endOfDay(dateTime).withDayOfMonth(dateTime.dayOfMonth().getMaximumValue());
}

private static DateTime beginningOfMonth(DateTime dateTime) {
    return beginningOfday(dateTime).withDayOfMonth(1);
}

/* Day */
private static DateTime endOfDay(DateTime dateTime) {
    return endOfHour(dateTime).withHourOfDay(23);
}

private static DateTime beginningOfday(DateTime dateTime) {
    return beginningOfHour(dateTime).withHourOfDay(0);
}

/* Hour */
private static DateTime beginningOfHour(DateTime dateTime) {
    return dateTime.withMillisOfSecond(0).withSecondOfMinute(0).withMinuteOfHour(0);
}

private static DateTime endOfHour(DateTime dateTime) {
    return dateTime.withMillisOfSecond(999).withSecondOfMinute(59).withMinuteOfHour(59);
}
A: 

Joda does supply DateMidnight and LocalDate that might alleviate having to deal with hours and minutes so much on the cases where you really only care about day boundaries. LocalDate also implements the interface ReadablePartial, and it might be worthwhile to look at making other implementations of that which reflect the fields of interest.

Don Roby
A: 

I know the java libraries are 0 based (ie January is 0, December is 11). I don't think Joda is, but if you're not sure double check that.