Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Dates
  4.  
  5. {
  6. public static void main(String[] args)
  7. {
  8. int month, day, year; //date read in from user
  9. int daysInMonth; //number of days in month read in
  10.  
  11. boolean monthValid, yearValid, dayValid; //true if input from user is valid
  12. boolean leapYear; //true if user's year is a leap year
  13.  
  14. Scanner scan = new Scanner(System.in);
  15.  
  16. System.out.print ("Enter Month as a number: ");
  17. month = scan.nextInt();
  18.  
  19. System.out.print ("Enter in the day:");
  20. day = scan.nextInt();
  21.  
  22. System.out.print ("Enter in the year in the 2nd-millennium:");
  23. year = scan.nextInt();
  24.  
  25. //Get integer month, day, and year from user
  26.  
  27. if (month >= 1 && month <=12)
  28. monthValid = true; //Check to see if month is valid
  29.  
  30. if (year >=1000 && year <= 1999)
  31. yearValid = true; //Check to see if year is valid
  32.  
  33. //Determine whether it's a leap year
  34.  
  35. if (year % 400 == 0)
  36. {
  37. leapYear = true;
  38. }
  39. else if (year % 4 == 0)
  40. {
  41. if (year %100 == 0)
  42. {
  43. leapYear = false;
  44. }
  45. else
  46. {
  47. leapYear = true;
  48. }
  49. }
  50.  
  51. //Determine number of days in month
  52.  
  53. daysInMonth = 31;
  54.  
  55. if (month == 4 || month == 6 || month == 9 || month == 11)
  56. {
  57. daysInMonth = 30;
  58. }
  59. else
  60. {
  61. if (month == 2 && leapYear == true)
  62. daysinmonth = 29;
  63. }
  64. else
  65. {
  66. daysinmonth = 28;
  67. }
  68. }
  69.  
  70. if (monthValid == false)
  71. daysInMonth = 0;
  72.  
  73. //User number of days in month to check to see if day is valid
  74.  
  75. if (day == daysInMonth)
  76. {
  77. dayValid = true;
  78. }
  79. else
  80. {
  81. dayValid = false;
  82. }
  83.  
  84.  
  85. //Determine whether date is valid and print appropriate message
  86.  
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement