Java Blog

 
 
 
class MyException extends Exception
{
    private int detail;
    private String str;

    MyException(int d,String s)
    {
        detail=d;
        str=s;
    }
    public String toString()
    {
        return str+\" MyException[\"+detail+\"]\";
    }
}
class checkDate
{
    String arrD[]=new String [10];
    int month,day,year,d;

    static int i=-1;

    checkDate(String a[])
    {
        arrD=a;
    }

    void check() throws MyException
    {
        String s;

        i++;
        System.out.println(arrD[i]);

        //Check month

        s=arrD[i].substring(0,2);
        month=Integer.parseInt(s);

        if(month>12)
            throw new MyException(month,\"month\");

        //Check day and year

        s=arrD[i].substring(3,5);
        day=Integer.parseInt(s);

        s=arrD[i].substring(6,8);
        year=Integer.parseInt(s);

        if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
            d=31;
        else if(month==4 || month==6 || month==9 || month==11)
            d=30;
        else
        {
            if(year%4==0)
                d=29;
            else
                d=28;
        }

        if(1>day || day > d)
            throw new MyException(day,\"day\");        
    }
    void format()
    {
        switch(month)
        {
            case 1:
                System.out.print(day+\" January \");
                break;
            case 2:
                System.out.print(day+\" February \");
                break;
            case 3:
                System.out.print(day+\" March \");
                break;
            case 4:
                System.out.print(day+\" April \");
                break;
            case 5:
                System.out.print(day+\" May \");
                break;
            case 6:
                System.out.print(day+\" Jun \");
                break;
            case 7:
                System.out.print(day+\" July \");
                break;
            case 8:
                System.out.print(day+\" August \");
                break;
            case 9:
                System.out.print(day+\" September \");
                break;
            case 10:
                System.out.print(day+\" October \");
                break;
            case 11:
                System.out.print(day+\" Nevember \");
                break;
            case 12:
                System.out.print(day+\" Disember \");
                break;
        }
        if(year >= 50 && year <= 99)
            System.out.print(\"19\"+year+\"\\n\");
        else if(year >= 1 && year <= 49)
        {
            if(year >= 1 && year <= 9)
                System.out.print(\"200\"+year+\"\\n\");
            else
                System.out.print(\"20\"+year+\"\\n\");
        }

    }
}


class Date
{
    public static void main(String args[])
    {
        String arrD[]=new String [10];
        arrD[0]=\"04/31/83\";
        arrD[1]=\"05/02/09\";
        arrD[2]=\"02/28/54\";
        arrD[3]=\"02/29/92\";
        arrD[4]=\"02/31/90\";
        arrD[5]=\"30/10/06\";
        arrD[6]=\"02/31/55\";
        arrD[7]=\"08/31/80\";
        arrD[8]=\"02/29/54\";
        arrD[9]=\"12/12/03\";

        checkDate d=new checkDate(arrD);

        System.out.println(\"----------------------------------\");
        for(int k=0;k<10;k++)
        {
            try
            {
                d.check();
                d.format();
            }catch(MyException e)
            {
                System.out.println(\"  Invalid \"+e);
            }

            System.out.println(\"----------------------------------\");
        }    
    }
}
/*
            Output

----------------------------------
04/31/83
  Invalid day MyException[31]
----------------------------------
05/02/09
2 May 2009
----------------------------------
02/28/54
28 February 1954
----------------------------------
02/29/92
29 February 1992
----------------------------------
02/31/90
  Invalid day MyException[31]
----------------------------------
30/10/06
  Invalid month MyException[30]
----------------------------------
02/31/55
  Invalid day MyException[31]
----------------------------------
08/31/80
31 August 1980
----------------------------------
02/29/54
  Invalid day MyException[29]
----------------------------------
12/12/03
12 Disember 2003
----------------------------------

*/