Find the day of the week of a given date using Java language
Author: Shahrzad Jahangirian
Language: Java (using Java 7)
Date Added: Dec 20, 2016
Description: After publishing many large Java Database Application in my Website, this time for end of 2016
I developed a small Java Program to Find the day of the week of a given date. I find this problem at
http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/flowcontrol.html#day_of_week. There is a solution for
this problem in that page, but I designed and developed my own solution as you see below before I looked at their solution for the problem.
This is the problem description exactly as you find in above web page:
Find the day of the week of a given date
degree of difficulty: 2
Implement a Java-method that prints out the day of the week for a given day (1..31), month (1..12) and year.
The day of the week of dates between March 1900 and February 2100 can be calculated as follows:
First, you have to calculate the total number of days from 1900/1/1 to the given date (see below, for details). Secondly, you divide this number by 7 with integer remainder: this now is the day of the week, with 0 as sunday, 1 as monday, etc.
To calculate the total number of days you have to implement the following steps:
Subtract 1900 from the given year and multiply the result by 365
Add the missing leaps years by adding (year - 1900) / 4.
If the year itself is a leap year and the month is January or February, you have to subtract 1 from the previous result.
Now add the all days of the year up to the given one to the result (in case of february always 28, because the additional day for a leap year already have been added).
Here some dates with the day of the week for testing your Java programm:
Easter Sunday: 1916/3/23, 2007/4/8, 2010/4/4
Ash Wednesday: 2006/3/1, 2007/2/21, 2010/2/17
How to Run:
I run this program in Windows 7 Command line Prompt with
Java(TM) SE Runtime Environment 7 on my computer as you see above
Compile and Run it as below:
C:\javacodes> javac DayWeek.java "compiles the DayWeek class"
C:\javacodes> Java DayWeek "and to run it"