In Java I would like to determine if a year is a leap year and display month, day, year with days of a week
Below is my code. I cannot display like this:
Enter month: May
Enter day: 2
Enter year: 2021
2021 is not a leap year
May 2, 2021 is Sunday
The month day year are connected to the days of a week. How? Please help.
import java.util.Scanner;
import java.util.Calendar;
public class nandyan3 {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
// year to be checked
int day, year; String month;
System.out.println("DAY OF THE DATE, YEAR IS A LEAP?");
Scanner smile = new Scanner(System.in);
boolean leap = false;
System.out.print("Enter Month: ");
month = smile.next();
System.out.print("Enter Day: ");
day= smile.nextInt();
String[] strDays = new String[]{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
System.out.print("Enter year: ");
year = smile.nextInt();
// if the year is divided by 4
if (year % 4 == 0) {
// if the year is century
if (year % 100 == 0) {
// if year is divided by 400
// then it is a leap year
if (year % 400 == 0)
leap = true;
else
leap = false;
}
// if the year is not century
else
leap = true;
}
else
leap = false;
if (leap)
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
System.out.println(month + " " + day + "," + " " + year + " " + "is" + " " + strDays[now.get(Calendar.DAY_OF_WEEK) - 1]
);
}
}