// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
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());
    }
}
