Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.InputMismatchException;
  3.  
  4. public class DateFormat
  5. {
  6. public static int monthNumber() throws MonthException
  7. {
  8. int month = 0;
  9. Scanner keyboard = new Scanner(System.in);
  10. try
  11. {
  12. System.out.println("Enter month number: ");
  13. month = keyboard.nextInt();
  14. if(month <= 0 || month > 12)
  15. throw new MonthException();
  16. }
  17. catch(InputMismatchException e)
  18. {
  19. throw new MonthException();
  20. }
  21. return month;
  22. }
  23.  
  24. public static int dayNumber() throws DayException
  25. {
  26. int day = 0;
  27. Scanner keyboard = new Scanner(System.in);
  28. try
  29. {
  30. System.out.println("Enter day number: ");
  31. day = keyboard.nextInt();
  32. if(day > 31 || day <= 0)
  33. throw new DayException();
  34. }
  35. catch(InputMismatchException e)
  36. {
  37. throw new DayException();
  38. }
  39. return day;
  40. }
  41.  
  42. public static int yearNumber() throws YearException
  43. {
  44. int year = 0;
  45. Scanner keyboard = new Scanner(System.in);
  46. try
  47. {
  48. System.out.println("Enter year number: ");
  49. year = keyboard.nextInt();
  50. if(year > 3000 || year <= 1000)
  51. throw new YearException();
  52. }
  53. catch(InputMismatchException e)
  54. {
  55. throw new YearException();
  56. }
  57. return year;
  58. }
  59.  
  60. public static String numToString(int month)
  61. {
  62. switch(month)
  63. {
  64. case 1:
  65. return "January";
  66. case 2:
  67. return "February";
  68. case 3:
  69. return "March";
  70. case 4:
  71. return "April";
  72. case 5:
  73. return "May";
  74. case 6:
  75. return "June";
  76. case 7:
  77. return "July";
  78. case 8:
  79. return "August";
  80. case 9:
  81. return "September";
  82. case 10:
  83. return "October";
  84. case 11:
  85. return "November";
  86. case 12:
  87. return "December";
  88. }
  89. }
  90.  
  91. public class MonthException extends Exception
  92. {
  93. public MonthException()
  94. {
  95. super("Incorrect month");
  96. }
  97. public MonthException(String message)
  98. {
  99. super(message);
  100. }
  101. }
  102.  
  103. public class DayException extends Exception
  104. {
  105. public DayException()
  106. {
  107. super("Invalid day.");
  108. }
  109. public DayException(String message)
  110. {
  111. super(message);
  112. }
  113. }
  114.  
  115.  
  116. public class YearException extends Exception
  117. {
  118. public YearException()
  119. {
  120. super("Invalid year.");
  121. }
  122.  
  123. public YearException(String message)
  124. {
  125. super(message);
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement