Pregunta

Estoy utilizando la siguiente cortado con tijeras para encontrar el inicio y el final de varios períodos de tiempo en Joda. El pequeño diablo en mi hombro izquierdo dice que esa es la manera de ir ... pero lo no creemos.

Podría alguien con alguna experiencia joda tomar un breve vistazo y dime que el pequeño individuo es correcto?

(Se ser utilizado para UTC de fecha y hora objetos)

Gracias!

/* 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);
}
¿Fue útil?

Solución

Joda hace la oferta y DateMidnight LocalDate que podría aliviar tener que lidiar con las horas y minutos tanto en los casos en los que realmente sólo se preocupan por los límites diarios. LocalDate también implementa la interfaz ReadablePartial, y que valdría la pena mirar lo que otras implementaciones de lo que reflejan los campos de interés.

Otros consejos

I sabe las bibliotecas de Java son 0 base (es decir, enero es 0, diciembre es 11). Creo que no es Joda, pero si no está seguro de que vuelva a comprobar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top