0

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]
            );
    
        }
    }
Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26
Cole Cole
  • 1
  • 2
  • `final Calendar now` is as the name suggest a variable you use to hold the current date/time as a Calendar Object. So in your last line when you use `now` to try to print the weekday of the entered date it is printing the weekday the program is run on instead. – OH GOD SPIDERS Apr 30 '21 at 14:12
  • so... do you know how to get the weekday of the entered month day year of user? i still cannot do it – Cole Cole May 02 '21 at 04:10

0 Answers0