Re: Searching for a "TimeInterval" type...
On 10/2/2013 9:36 PM, Arne Vajh??j wrote:
On 10/2/2013 11:12 AM, Andreas Leitgeb wrote:
Arne Vajh??j <arne@vajhoej.dk> wrote:
I would be tempted to go for the simple solution with a class
that contains two ints: number of units and a Calendar
unit identifier.
Unfortunately, that would be too simple for my needs.
Some example interval of what I'm going to deal with can be
described as "tomorrow one (wall-clock)hour earlier". This just
isn't the same as in "23 hours" (nor as "82800 seconds").
Maybe two classes RelativeTime and AbsoluteTime so your
problem could be one RelativeTime with 1 HOUR and one
AbsoluteTime with HOUR X (where X is now hour minus one).
Illustration:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class TimeManipulation {
public static interface TimeUpdate {
public void applyTo(Calendar cal);
}
public static class AbsoluteTimeUpdate implements TimeUpdate {
private int value;
private int field;
public AbsoluteTimeUpdate(int value, int field) {
this.value = value;
this.field = field;
}
@Override
public void applyTo(Calendar cal) {
cal.set(field, value);
}
}
public static class RelativeTimeUpdate implements TimeUpdate {
private int value;
private int field;
public RelativeTimeUpdate(int value, int field) {
super();
this.value = value;
this.field = field;
}
@Override
public void applyTo(Calendar cal) {
cal.add(field, value);
}
}
private List<TimeUpdate> upds;
public TimeManipulation(TimeUpdate... upds) {
this.upds = new ArrayList<TimeUpdate>();
for(TimeUpdate upd : upds) this.upds.add(upd);
}
public void applyTo(Calendar cal) {
for(TimeUpdate upd : upds) {
upd.applyTo(cal);
}
}
public static void main(String[] args) {
TimeManipulation tm = new TimeManipulation(new RelativeTimeUpdate(1,
Calendar.DAY_OF_MONTH),
new RelativeTimeUpdate(-1,
Calendar.HOUR_OF_DAY));
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime());
tm.applyTo(cal);
System.out.println(cal.getTime());
}
}
Arne