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);
}
4

2 回答 2

1

Joda 确实提供了 DateMidnight 和 LocalDate ,这可能会减轻在您真正只关心日期边界的情况下不得不处理时间和分钟的问题。LocalDate 还实现了 ReadablePartial 接口,可能值得研究一下其他反映感兴趣领域的实现。

于 2010-03-22T22:19:04.707 回答
0

我知道 Java 库是基于 0 的(即 1 月是 0,12 月是 11)。我不认为 Joda 是,但如果你不确定,请仔细检查。

于 2010-03-22T22:53:05.637 回答