The user provides us with three integer inputs: day, month, and year. Our goal is to use the given date to determine the day name, such as Monday, Tuesday, Wednesday, etc.
For example:
Input: 21 03 2024
Output: Thursday
Zeller's congruence Algorithm
January and February are considered as months 13 and 14 of the previous year in this method of calculation. For instance, the algorithm considers February 2, 2010, as the second day of month 14 i.e.,聽(02/14/2009 in DD/MM/YYYY notation)
.
-
h
is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday) -
q
is the day of the month -
m
is the month (3 = March, 4 = April, 5 = May, ..., 14 = February) -
K
the year of the century -
J
is the zero-based century (For example, the zero-based centuries for 1995 and 2000 are 19 and 20 respectively)
Solution in Java:
import java.util.Scanner;
class DayNameFinder{
public static void main(String x[]){
Scanner scan = new Scanner(System.in);
DayNameFinder df = new DayNameFinder();
System.out.println("Enter the date (day, month, year) in numbers:");
int day = scan.nextInt();
int month = scan.nextInt();
int year = scan.nextInt();
System.out.println("The day name is: " + df.findDay(day, month, year));
}
public String findDay(int dayValue, int monthValue, int yearValue){
if(monthValue == 1 || monthValue == 2){
monthValue += 12;
yearValue--;
}
int yearOfCentury = yearValue % 100;
int century = yearValue / 100;
int result = (dayValue + (13 * (monthValue + 1)/5 + yearOfCentury + yearOfCentury/4 + century/4 + 5*century)) % 7;
String[] dayName = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
return dayName[result];
}
}
In many date calculation algorithms, including the one used in this program, Saturday
is often considered the first day of the week for computational purposes. This convention is based on international standards (ISO 8601)
and cultural norms prevalent in many parts of the world.
It provides consistency across different applications and programming environments, making it easier for developers to implement date-related functionalities.
On successful compilation, if we run the program we will get the output as:
Thank you for reading.
馃槑