[go: up one dir, main page]

0% found this document useful (0 votes)
20 views3 pages

Calendar

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Calendar

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;
public class Calendar
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

int year, month, day;

// validate year input


while (true) {
System.out.println("Enter a year: ");
year = scan.nextInt();
if (year > 0) {
break;
} else {
System.out.println("Invalid year, please enter a positive year.");
}
}

// Validate month input


while (true) {
System.out.print("Enter a month (1-12): ");
month = scan.nextInt();
if (month >= 1 && month <= 12) {
break;
} else {
System.out.println("Invalid month. Please enter a month between 1
and 12.");
}
}

// adjust month for January and February


if (month == 1) {
month = 13;
year -= 1;
} else if (month == 2) {
month = 14;
year -= 1;
}

// validate day input


int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
while (true) {
System.out.print("Enter a day (1-" + daysInMonth[month - 1] + "): ");
day = scan.nextInt();
if (day >= 1 && day <= daysInMonth[month - 1]) {
break;
} else {
System.out.println("Invalid day. Please enter a day between 1 and "
+ daysInMonth[month - 1] + ".");
}
}

// calculate day of the week using Zeller's congruence


int q = day;
int m = month;
int k = year % 100;
int j = year / 100;
int h = (q + 26 * (m + 1) / 10 + k + k / 4 + j / 4 + 5 * j) % 7;

String[] daysOfWeek = {"Saturday", "Sunday", "Monday", "Tuesday",


"Wednesday", "Thursday", "Friday"};
String dayOfWeek = daysOfWeek[h];

// Determine tense based on current year


int currentYear = 2024; // Replace with current year
String tense;
if (year < currentYear) {
tense = "was";
} else if (year > currentYear) {
tense = "will be";
} else {
tense = "is";
}

// print result
String monthName;
switch (month) {
case 13:
monthName = "January";
break;
case 14:
monthName = "February";
break;
default:
monthName = getMonthName(month);
}
System.out.println(monthName + " " + day + ", " + year + " " + tense + " a "
+ dayOfWeek + "!");
}

private static String getMonthName(int month)


{
switch (month)
{
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "";
}
}
}

You might also like