Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Date {
  4. private int day;
  5. private int month;
  6. private int year;
  7. private int daysInMonth;
  8.  
  9. Scanner in = new Scanner(System.in);
  10.  
  11. public Date() {
  12. day = 1;
  13. month = 1;
  14. year = 2010;
  15.  
  16. }
  17.  
  18. public int getDaysInMonth(int month) {
  19.  
  20. switch (month) {
  21.  
  22. case 1:
  23. daysInMonth = 31;
  24.  
  25. break;
  26.  
  27. case 2:
  28. daysInMonth = 28;
  29.  
  30. break;
  31.  
  32. case 3:
  33. daysInMonth = 31;
  34.  
  35. break;
  36.  
  37. case 4:
  38. daysInMonth = 30;
  39.  
  40. break;
  41.  
  42. case 5:
  43. daysInMonth = 31;
  44.  
  45. break;
  46.  
  47. case 6:
  48. daysInMonth = 30;
  49.  
  50. break;
  51.  
  52. case 7:
  53. daysInMonth = 31;
  54.  
  55. break;
  56.  
  57. case 8:
  58. daysInMonth = 31;
  59.  
  60. break;
  61.  
  62. case 9:
  63. daysInMonth = 30;
  64.  
  65. break;
  66.  
  67. case 10:
  68. daysInMonth = 31;
  69.  
  70. break;
  71.  
  72. case 11:
  73. daysInMonth = 30;
  74.  
  75. break;
  76.  
  77. case 12:
  78. daysInMonth = 31;
  79.  
  80. break;
  81.  
  82. }
  83.  
  84. return daysInMonth;
  85. }
  86.  
  87. public void setDate() {
  88.  
  89. System.out.println("Enter a day");
  90. day = in.nextInt();
  91. while (day <= 0 || day > 31) {
  92. System.out.println("Please Enter a valid day");
  93. day = in.nextInt();
  94. }
  95.  
  96. System.out.println("Enter a month");
  97. month = in.nextInt();
  98. while (month <= 0 || month > 12) {
  99. System.out.println("Please Enter a valid month");
  100. month = in.nextInt();
  101. }
  102. getDaysInMonth(month);
  103.  
  104. while (day > daysInMonth) {
  105. System.out.println("The month you entered does not have as many days. Please Enter a valid month");
  106. month = in.nextInt();
  107. getDaysInMonth(month);
  108.  
  109. }
  110.  
  111. System.out.println("Enter a year");
  112. year = in.nextInt();
  113.  
  114. while (year < 1900 || year > 9999) {
  115. System.out.println("Please Enter a valid year");
  116. year = in.nextInt();
  117. }
  118. }
  119.  
  120. public void displayDate() {
  121. System.out.println("You have entered: " + day + "/" + month + "/" + year);
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement