Advertisement
cgorrillaha

Untitled

Oct 27th, 2021
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. // ****************************************************************
  2. // Dates.java
  3. //
  4. // Determine whether a 2nd-millenium date entered by the user
  5. // is valid
  6. // ****************************************************************
  7. import java.util.Scanner;
  8.  
  9. public class Dates
  10. {
  11.     public static void main(String[] args)    {
  12.         int month, day, year;   //date read in from user
  13.         int daysInMonth;        //number of days in month read in
  14.         boolean monthValid, yearValid, dayValid;  //true if input from user is valid
  15.         boolean leapYear;       //true if user's year is a leap year
  16.  
  17.         Scanner scan = new Scanner(System.in);
  18.  
  19.         //Get integer month, day, and year from user
  20.         System.out.println("Please enter the date: month day year");
  21.         System.out.println("what is the month?");
  22.         month= scan.nextInt();
  23.         System.out.println("what is the day?");
  24.         day= scan.nextInt();
  25.         System.out.println("what is the year?");
  26.         year= scan.nextInt();
  27.  
  28.         int num=772;
  29.         boolean even7=num%7==0;
  30.         System.out.println(even7);
  31.         System.out.println(num%7);
  32.  
  33.         //Check to see if month is valid
  34.         monthValid=month>0&&month<=12;
  35.         //Check to see if year is valid
  36.         yearValid=year>=1000&&year<=1999;
  37.         //Determine whether it's a leap year
  38.         //it's a leap year if a) it's divisible by 400, or b) it's divisible by 4 and it's not divisible by 100.
  39.         leapYear=year%400==0||(year%4==0&&year%100!=0);
  40.         //Determine number of days in month
  41.         /*
  42.             Thirty days has September,
  43.             April, June, and November,
  44.             All the rest have thirty-one,
  45.             Save February at twenty-eight,
  46.             But leap year, coming once in four,
  47.             February then has one day more.
  48.         */
  49.         if(month==9||month==4||month==6||month==11){
  50.             daysInMonth=30;
  51.         }else if(month==2){//feb
  52.             if(leapYear)
  53.                 daysInMonth=29;
  54.             else{
  55.                 daysInMonth=28;
  56.             }
  57.         }else{//all the rest
  58.             daysInMonth=31;
  59.         }
  60.  
  61.         //User number of days in month to check to see if day is valid
  62.         dayValid=day<=daysInMonth&&day>0;
  63.         //Determine whether date is valid and print appropriate message
  64.         if(dayValid&&yearValid&&monthValid){
  65.             System.out.printf("%d/%d/%d is a valid date!%n", month, day, year);
  66.         }else{
  67.             System.out.printf("%d/%d/%d is not a valid date!%n", month, day, year);
  68.         }
  69.     }
  70. }
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement