A
Andreas Leitgeb
My application will maintain Calendar instances, and I need another type
to hold "intervals", such as (5 months, 4 days and 30 minutes) that I
could conveniently "add" to a Calendar instance.
My first thought was using another Calendar, and do a loop for the
add-operation:
Calendar cal = ...; Calendar intvl = ...;
for (int i=0; i<Calendar.FIELD_COUNT; i++) {
if (intvl.isSet(i)) { cal.add(i,intvl.get(i)); }
}
which would be fine for simple cases, but wouldn't work, if I
wanted to represent "100 days", which definitely isn't equivalent
to "3 months and 10/9 days" (depending on leapyear). If I just
don't call "complete()", then according to the docs get() would
bomb on me, for the days-value being out of bounds.
One alternative could be to just wrap a new int[Calendar.FIELD_COUNT],
and use the Calendar-constants to index into the array, but I wonder,
if I'm perhaps missing something more elegant. It's hard for me to
believe, that there wasn't any common need for representing intervals
of time in a mixture of units, and not just seconds.
Right now I'm optimizing for elegance not for performance.
to hold "intervals", such as (5 months, 4 days and 30 minutes) that I
could conveniently "add" to a Calendar instance.
My first thought was using another Calendar, and do a loop for the
add-operation:
Calendar cal = ...; Calendar intvl = ...;
for (int i=0; i<Calendar.FIELD_COUNT; i++) {
if (intvl.isSet(i)) { cal.add(i,intvl.get(i)); }
}
which would be fine for simple cases, but wouldn't work, if I
wanted to represent "100 days", which definitely isn't equivalent
to "3 months and 10/9 days" (depending on leapyear). If I just
don't call "complete()", then according to the docs get() would
bomb on me, for the days-value being out of bounds.
One alternative could be to just wrap a new int[Calendar.FIELD_COUNT],
and use the Calendar-constants to index into the array, but I wonder,
if I'm perhaps missing something more elegant. It's hard for me to
believe, that there wasn't any common need for representing intervals
of time in a mixture of units, and not just seconds.
Right now I'm optimizing for elegance not for performance.