Advertisement
Guest User

Untitled

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