Wednesday, May 9, 2012

Program to calculate the day using dates



import java.io.*;
public class day_calculator
{
    int date,month,year;
    String day,s;
    int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
    int y[]={31,59,90,120,151,181,212,243,273,304,334,365};
    String d[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
    private void input()throws IOException
    {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter Date(dd/mm/yyyy):");
       s=br.readLine();
       date=Integer.parseInt(s.substring(0,2));
       month=Integer.parseInt(s.substring(3,5));
       year=Integer.parseInt(s.substring(6));
       System.out.print("Enter the day on 1st January:");
       day=br.readLine();
    }
    private void calculate()
    {
        int nod=0;
        if(year%4!=0)
        nod=y[month-1]-(m[month-1]-date);
        else if(year%4==0)
        {
            m[1]=29;
            y[11]=366;
        nod=y[month-1]-(m[month-1]-date);
    }
        int mod=nod%7;
        int add=-1;
        for(int i=0;i<7;i++)
        {
            if(d[i].equalsIgnoreCase(day)==true)
            add=i;
        }
        if(add==-1)
        {
        System.out.print("Error in day or date....please re-enter");
        System.exit(0);
    }
        add=add+mod;
        if(add!=0)
        add--;
        System.out.print("Day on:"+s+":"+d[add]);
    }
    public static void main(String args[])throws IOException
    {
        day_calculator obj=new day_calculator();
        obj.input();
        obj.calculate();
    }
}
   

     

Reactions:

0 comments:

Post a Comment