我正在使用以下片段在 Joda 中查找几个时间段的开始和结束。我左肩上的小恶魔说那是要走的路……但我不相信他。
任何有 joda 经验的人都可以简单看一下并告诉我这个小家伙是对的吗?
(它将仅用于 UTC 日期时间对象)
谢谢!
/* 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);
}
最佳答案
Joda 确实提供了 DateMidnight 和 LocalDate,在您真正只关心日期界限的情况下,它们可能会减轻处理小时和分钟的麻烦。 LocalDate 还实现了 ReadablePartial 接口(interface),可能值得考虑制作反射(reflect)感兴趣领域的其他实现。
关于java - 查找年/月/日/小时的开始和结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2494201/