Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = false, yearValid = false, dayValid; //true if input from user is valid
- boolean leapYear = false; //true if user's year is a leap year
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter Month as a number: ");
- month = scan.nextInt();
- System.out.print("Enter in the day:");
- day = scan.nextInt();
- System.out.print("Enter in the year in the 2nd-millennium:");
- year = scan.nextInt();
- //Get integer month, day, and year from user
- if (month >= 1 && month <= 12) {
- monthValid = true; //Check to see if month is valid
- }
- if (year >= 1000 && year <= 1999) {
- yearValid = true; //Check to see if year is valid
- }
- //Determine whether it's a leap year
- if (year % 400 == 0) {
- leapYear = true;
- } else if (year % 4 == 0) {
- if (year % 100 == 0) {
- leapYear = false;
- } else {
- leapYear = true;
- }
- }
- //Determine number of days in month
- daysInMonth = 31;
- if (month == 4 || month == 6 || month == 9 || month == 11) {
- daysInMonth = 30;
- } else {
- if (month == 2 && leapYear == true) {
- daysInMonth = 29;
- } else {
- daysInMonth = 28;
- }
- }
- if (monthValid == false) {
- daysInMonth = 0;
- }
- //User number of days in month to check to see if day is valid
- if (day <= daysInMonth) {
- dayValid = true;
- } else {
- dayValid = false;
- }
- //Determine whether date is valid and print appropriate message
- if (monthValid == true && yearValid == true && dayValid == true) {
- System.out.println("Date is valid.");
- if (leapYear) {
- System.out.println("Is Leap year.");
- }
- } else {
- System.out.println("Date is not valid.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement