Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ****************************************************************
- // Congminh Tran
- // 10/23/19
- //
- // Dates.java
- //
- // Determine whether a 2nd-millenium date entered by the user
- // is valid
- //
- // ****************************************************************
- import java.util.Scanner; //Import packages and methods from Scanner class
- public class Dates
- {
- public static void main (String[] args)
- {
- int month, day, year; //date read in from user
- int daysInMonth; //number of days in month read in
- boolean monthValid, yearValid, dayValid; //true if input from user is valid
- boolean leapYear; //true if user's year is a leap year
- Scanner scan = new Scanner(System.in);
- //Get integer month, day, and year from user
- System.out.print("Enter month: ");
- month = scan.nextInt();
- System.out.print("Enter day: ");
- day = scan.nextInt();
- System.out.print("Enter year: ");
- year = scan.nextInt();
- //Check to see if month is valid, set monthValid accordingly
- if (month <= 12 && month >= 1)
- monthValid = true;
- //Check to see if year is valid, set yearValid accordingly
- if (year <= 1999 && year >= 1000)
- yearValid = true;
- //Determine whether it's a leap year, set leapYear accordingly
- if ((year % 400) == 0 || ((year % 4) == 0 && (year % 100) != 0))
- leapYear = true;
- //Set daysInMonth based on what month user entered.
- if ((month % 2) == 1 || month == 8 || month == 10 || month == 12)
- daysInMonth = 31;
- else if ((month % 2) == 0 && month != 2 || month == 9 || month == 11)
- daysInMonth = 30;
- else
- {
- if (leapYear = true)
- daysInMonth = 29;
- else
- daysInMonth = 28;
- }
- //Determine whether day entered is valid, based on value set in daysInMonth.
- if (day <= daysInMonth && day >= 1)
- dayValid = true;
- //Determine whether ENTIRE date is valid and print appropriate message
- if (dayValid == true && monthValid == true && yearValid == true)
- System.out.println(month + "/" + day + "/" + year +"is a valid date.");
- //If date is valid only: Print if date is a leap year. DO NOT PRINT LEAP YEAR STATUS IF DATE IS INVALID
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement