We have 7 days in a week. And the first Day of the Date System is Sunday. And it should be. So, 01.01.0001, i.e.,
First Date of Christ was Sunday. We just number the days as follows.
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednessday
4 = Thursday
5 = Friday
6 = Saturday
So, we can handle every day by the reminder of 7. Cause, u just see, after every 7 days the same day is repeated.
Then, lets a rough Calculation.
01/01/0001 => Sunday = 0
Now, we just try to find out the day on 1st January on next year, i.e., 01/01/0002
0001 has 365 days.
So, after 365 days which day will come? To find out the answer of this question we have to perform following calculation.
But, using this method, it is very critical to find out the day of 1st January of 1988. So, we have an another idea.
Find out how many Leap years are present before current year. Suppose it is L.
So,
Now, find out the day number by dividing the expression (y-1+L) by 7 and take the reminder as Day Number.
OK, Let's take a test. Suppose date is 1.1.2009
y = 2009
L = 2009/4 = 502
So the Day Number will be = ( 2009 - 1 + L ) % 7 = 2510 % 7 = 4
The Day is:: Thursday
Now the above Logic is implemented by following Java Program.
It may be a usefull sub program for Calender based Software......
First Date of Christ was Sunday. We just number the days as follows.
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednessday
4 = Thursday
5 = Friday
6 = Saturday
So, we can handle every day by the reminder of 7. Cause, u just see, after every 7 days the same day is repeated.
Then, lets a rough Calculation.
01/01/0001 => Sunday = 0
Now, we just try to find out the day on 1st January on next year, i.e., 01/01/0002
0001 has 365 days.
So, after 365 days which day will come? To find out the answer of this question we have to perform following calculation.
7 ) 365 ( 52 35 ----- 15 14 ---- 1The reminder is 1, i.e.,Monday.
But, using this method, it is very critical to find out the day of 1st January of 1988. So, we have an another idea.
Find out how many Leap years are present before current year. Suppose it is L.
So,
L = y/4 | If Current year is not a Leap Year |
L = (y/4) -1 | If Current year is a Leap Year |
OK, Let's take a test. Suppose date is 1.1.2009
y = 2009
L = 2009/4 = 502
So the Day Number will be = ( 2009 - 1 + L ) % 7 = 2510 % 7 = 4
The Day is:: Thursday
Now the above Logic is implemented by following Java Program.
import java.lang.*; import java.io.*; class Calender { public static void main(String args[]) throws IOException{ String day[]={"SUNDAY","MONDAY","TUESDAY","WEDNESSDAY","THURSDAY","FRIDAY","SATURDAY"}; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a Date (in format dd/mm/yyyy):: "); String dt=in.readLine(); if(dt.length()!=10) { System.out.println("Invalid Entry ! ! !"); return; } int d=Integer.parseInt(dt.substring(0,2)); if(d<1 || d>31) return; int m=Integer.parseInt(dt.substring(3,5)); if(m<1 || m>12 ) return; int y=Integer.parseInt(dt.substring(6,10)); if(y<1) return; int f,l; if(y%4==0 || y%400==0) { f=29; l=(y/4)-1; } else { f=28; l=y/4; } int days[] = {31,f,31,30,31,30,31,31,30,31,30,31}; f=(y-1+l)%7; int totd=0; for(int i=0;i<m-1;i++) totd+=days[i]; totd+=d; totd+=(f-1); System.out.println("The Day is :: " + day[totd%7]); } }
Comments