SHARE
TWEET

Dates.java

a guest Oct 23rd, 2019 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ****************************************************************
  2. // Congminh Tran
  3. // 10/23/19
  4. //
  5. // Dates.java
  6. //
  7. // Determine whether a 2nd-millenium date entered by the user
  8. // is valid
  9. //          
  10. // ****************************************************************
  11. import java.util.Scanner;  //Import packages and methods from Scanner class
  12.  
  13. public class Dates
  14. {
  15.    public static void main (String[] args)
  16.    {
  17.       int month, day, year;   //date read in from user
  18.       int daysInMonth;        //number of days in month read in
  19.       boolean monthValid, yearValid, dayValid;  //true if input from user is valid
  20.       boolean leapYear;  //true if user's year is a leap year
  21.    
  22.       Scanner scan = new Scanner(System.in);
  23.    
  24.       //Get integer month, day, and year from user
  25.       System.out.print("Enter month: ");
  26.       month = scan.nextInt();
  27.      
  28.       System.out.print("Enter day: ");
  29.       day = scan.nextInt();
  30.      
  31.       System.out.print("Enter year: ");
  32.       year = scan.nextInt();
  33.      
  34.       //Check to see if month is valid, set monthValid accordingly
  35.       if (month <= 12 && month >= 1)
  36.          monthValid = true;
  37.    
  38.       //Check to see if year is valid, set yearValid accordingly
  39.       if (year <= 1999 && year >= 1000)
  40.          yearValid = true;
  41.    
  42.       //Determine whether it's a leap year, set leapYear accordingly
  43.       if ((year % 400) == 0 || ((year % 4) == 0 && (year % 100) != 0))
  44.          leapYear = true;
  45.    
  46.       //Set daysInMonth based on what month user entered.
  47.       if ((month % 2) == 1 || month == 8 || month == 10 || month == 12)
  48.          daysInMonth = 31;
  49.       else if ((month % 2) == 0 && month != 2 || month == 9 || month == 11)
  50.          daysInMonth = 30;
  51.       else
  52.       {
  53.          if (leapYear = true)
  54.             daysInMonth = 29;
  55.          else
  56.             daysInMonth = 28;
  57.       }
  58.      
  59.       //Determine whether day entered is valid, based on value set in daysInMonth.
  60.       if (day <= daysInMonth && day >= 1)
  61.          dayValid = true;
  62.    
  63.       //Determine whether ENTIRE date is valid and print appropriate message
  64.       if (dayValid == true && monthValid == true && yearValid == true)
  65.          System.out.println(month + "/" + day + "/" + year +"is a valid date.");
  66.          
  67.    
  68.       //If date is valid only: Print if date is a leap year. DO NOT PRINT LEAP YEAR STATUS IF DATE IS INVALID
  69.    }
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top