Finding begin and end of year/month/day/hour
Posted
by reto
on Stack Overflow
See other posts from Stack Overflow
or by reto
Published on 2010-03-22T17:05:46Z
Indexed on
2010/03/22
22:21 UTC
Read the original article
Hit count: 787
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);
}
© Stack Overflow or respective owner