/**
 * Date.java
 * @date Jan 2, 2011
 * @author ricky barrette
 * @author Twenty Codes, LLC
 */
package com.TwentyCodes.java.OrderProcessor;

import java.io.Serializable;

/**
 * A Date object that represents a specific date and time
 * @author ricky barrette
 */
public class Date implements Comparable<Date>, Serializable{

	private static final long serialVersionUID = 5691938526283433531L;
	private byte mHour;
	private byte mMinute;
	private byte mSecond;
	private byte mMonth;
	private byte mDay;
	private int mYear;

	/**
	 * Creates a new Date from a string. 
	 * The string format will be 'YYYY-MM-DD HH:MM:SS' example: '2010-12-30 22:24:08'
	 * @param time
	 * @author ricky barrette
	 * @throws InvalidDateFormatException 
	 */
	public Date(String newDate) throws InvalidDateFormatException {
		String[] parts = newDate.split(" ");
		String[] date = parts[0].split("-");
		String[] time = parts[1].split(":");
		try {
			this.mYear = Integer.parseInt(date[0]);
			this.mDay  = Byte.parseByte(date[2]);
			this.mMonth = Byte.parseByte(date[1]);
			this.mHour = Byte.parseByte(time[0]);
			this.mMinute = Byte.parseByte(time[1]);
			this.mSecond = Byte.parseByte(time[2]);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			throw new InvalidDateFormatException(newDate);
		}
	}

	/**
	 * returns a human readable string that reprensts this object
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 * @author ricky barrette
	 */
	@Override
	public String toString(){
		return this.mHour +":"+ this.mMinute +":"+ this.mSecond +" "+ this.mMonth +"-"+ this.mDay +"-"+ this.mYear;
	}
	

	@Override
	public int compareTo(Date date) {
//		if(Main.DEBUG)
//			System.out.println("compareTo: "+ this.toString() +" to "+ date.toString());
		
		if(this.mYear != date.getYear())
			return this.mYear > date.getYear() ? 1 : -1;
		else
			if(this.mMonth != date.getMonth())
				return this.mMonth > date.getMonth() ? 1 : -1;
			else
				if(this.mDay != date.getDay())
					return this.mDay > date.getDay() ? 1 : -1;
				else
					if(this.mHour != date.getHour())
						return this.mHour > date.getHour() ? 1 : -1;
					else
						if(this.mMinute != date.getMinute())
							return this.mMinute > date.getMinute() ? 1 : -1;
						else
							if(this.mSecond != date.getSecond())
								return this.mSecond > date.getSecond() ? 1 : -1;
							else
								return 0;
						
	}

	/**
	 * getter for second
	 * @return seconds
	 * @author ricky barrette
	 */
	public byte getSecond() {
		return this.mSecond;
	}

	/**
	 * getter for minutes
	 * @return minutes
	 * @author ricky barrette
	 */
	public byte getMinute() {
		return this.mMinute;
	}

	/**
	 * getter for hours
	 * @return hours
	 * @author ricky barrette
	 */
	public byte getHour() {
		return this.mHour;
	}

	/**
	 * getter for day
	 * @return day
	 * @author ricky barrette
	 */
	public byte getDay() {
		return this.mDay;
	}

	/**
	 * getter for month
	 * @return month
	 * @author ricky barrette
	 */
	public byte getMonth() {
		return this.mMonth;
	}

	/**
	 * getter for year
	 * @return year
	 * @author ricky barrette
	 */
	public int getYear() {
		return this.mYear;
	}
}