/*
Copyright 2008, mark turansky (www.markturansky.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

(This is the license from www.json.org and I think it's awesome ~mgt)
*/
package mgt;

import java.util.Calendar;
import java.util.Date;

public class Dates {

    public static Date january(int day, int year){
        return create(Calendar.JANUARY, day, year);
    }

    public static Date february(int day, int year){
        return create(Calendar.FEBRUARY, day, year);
    }

    public static Date march(int day, int year){
        return create(Calendar.MARCH, day, year);
    }

    public static Date april(int day, int year){
        return create(Calendar.APRIL, day, year);
    }

    public static Date may(int day, int year){
        return create(Calendar.MAY, day, year);
    }

    public static Date june(int day, int year){
        return create(Calendar.JUNE, day, year);
    }

    public static Date july(int day, int year){
        return create(Calendar.JULY, day, year);
    }

    public static Date august(int day, int year){
        return create(Calendar.AUGUST, day, year);
    }

    public static Date september(int day, int year){
        return create(Calendar.SEPTEMBER, day, year);
    }

    public static Date october(int day, int year){
        return create(Calendar.OCTOBER, day, year);
    }

    public static Date november(int day, int year){
        return create(Calendar.NOVEMBER, day, year);
    }

    public static Date december(int day, int year){
        return create(Calendar.DECEMBER, day, year);
    }

    private static Date create(int month, int day, int year){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DATE, day);
        cal.set(Calendar.YEAR, year);
        return cal.getTime();
    }

    public static int getAge(Date dob){

        Calendar today = Calendar.getInstance();
        int year = today.get(Calendar.YEAR);

        Calendar dateOfBirth = Calendar.getInstance();
        dateOfBirth.setTime(dob);
        int dobYear = dateOfBirth.get(Calendar.YEAR);

        // this is only good if the Date hasn't had their birthday
        // yet this year.  might have to decrement one.
        int delta = year - dobYear;

        // birthday in the future.  decrement and return.
        if(dateOfBirth.get(Calendar.MONTH) > today.get(Calendar.MONTH)){
            return --delta;
        }

        // birthday already passed.  return the age.
        if(dateOfBirth.get(Calendar.MONTH) < today.get(Calendar.MONTH)){
            return delta;
        }

        // last remaining case is birthday month == today's month.  check date.
        if(dateOfBirth.get(Calendar.DATE) > today.get(Calendar.DATE)){
            // day of month is in the future.
            return --delta;
        } else {
            return delta;
        }

    }

    public static void main(String args[]){
        System.out.println(getAge(march(25, 1973)));
        System.out.println(getAge(august(28, 1973)));
    }

}