package example;

public class FieldDate extends Date {
	private int value;

	public FieldDate(int day, int month, int year) {
		assert((day >= 1) && (day <= 31) && (month >= 1) && (month <= 12) && (year >= 1));
		this.value = computeValue(day, month, year);
	}

	private static int computeValue(int day, int month, int year) {
//		return 512 * year + 32 * month + day;
		return year << 9 | month << 5 | day;
	}
	
	@Override
	public int year() {
//		return this.value / 512;
		return this.value >> 9;
	}

	@Override
	public int month() {
//		return (this.value % 512) / 32;
		return (this.value >> 5) & 0xF;
	}

	@Override
	public int day() {
//		return this.value % 32;
		return this.value & 0x1F;
	}

	@Override
	public int compareTo(Date d) {
		return this.value - computeValue(d.day(), d.month(), d.year());
	}

	@Override
	public String toString() {
		return this.day() + "/" + this.month() + "/" + this.year();
	}
	
	public static void main(String[] args) {
		Date era = new FieldDate(1,1,1970);
		Date d = new FieldDate(13,2,2012);
		
		System.out.print(d);
		int t = d.compareTo(era);
		if (t < 0) {
			System.out.print(" is before ");
		} else if (t > 0) {
			System.out.print(" is after ");
		} else {
			System.out.print(" is same as ");
		}
		System.out.println(era);
	}

}
