Advertisement
Guest User

date

a guest
Dec 10th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. package programming.set6.date;
  2.  
  3. /**
  4. * This class named date is a program that should return a date in a valid and
  5. * given format.
  6. *
  7. * @author Marielouise
  8. *
  9. */
  10. public class Date {
  11. private int year;
  12. private int month;
  13. private int day;
  14. private static final String[] MONTHS = { "January", "February", "March",
  15. "April", "May", "June", "July", "August", "September", "October",
  16. "November", "December" };
  17.  
  18. /**
  19. * The method getDaysInMonth have two parameters.
  20. *
  21. * @param year
  22. * in which we are counting.
  23. * @param month
  24. * that we are counting the days.
  25. * @return the number of days of a given year.
  26. */
  27.  
  28. public static int getDaysInMonth(int year, int month) {
  29.  
  30. switch (month) {
  31.  
  32. case 1: // "January";
  33. case 3: // "March";
  34. case 5: // "May";
  35. case 7: // "July";
  36. case 8: // "August";
  37. case 10: // "October";
  38. case 12: // "December";
  39. return 31;
  40.  
  41. case 4: // "April";
  42. case 6: // "June";
  43. case 9: // "September";
  44. case 11: // "November";
  45. return 30;
  46.  
  47. case 2: // "February";
  48. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
  49. return 29;
  50. } else {
  51. return 28;
  52. }
  53. default: // out of range
  54. return 0;
  55. }
  56.  
  57. }
  58.  
  59. /**
  60. * This is the method validate with three parameters of type integer.
  61. *
  62. * @param year
  63. * of the date to validate.
  64. * @param month
  65. * of the date to validate.
  66. * @param day
  67. * of the date to validate.
  68. * @return a valid date.
  69. */
  70. public static boolean validate(int year, int month, int day) {
  71. // boolean validate == true;
  72. if (month < 1 || month > 12) {
  73. return false;
  74. } else if (day < 1 || day > getDaysInMonth(year, month)) {
  75. return false;
  76. } else {
  77. return true;
  78. }
  79.  
  80. }
  81.  
  82. /**
  83. * This method return a date.
  84. *
  85. * @param year
  86. * of the date.
  87. * @param month
  88. * of the date
  89. * @param day
  90. * of the date.
  91. */
  92.  
  93. public Date(int year, int month, int day) {
  94. if (validate(year, month, day)) {
  95. this.year = year;
  96. this.month = month;
  97. this.day = day;
  98. } else {
  99. throw new IllegalArgumentException("This is not a valid date.");
  100.  
  101. }
  102. }
  103.  
  104. /**
  105. * This method get the day.
  106. *
  107. * @return a given day.
  108. */
  109. public int getDay() {
  110. return day;
  111. }
  112.  
  113. /**
  114. * This method get the month.
  115. *
  116. * @return a given month.
  117. */
  118. public int getMonth() {
  119. return month;
  120. }
  121.  
  122. /**
  123. * This method get the year.
  124. *
  125. * @return a given year.
  126. */
  127. public int getYear() {
  128. return year;
  129. }
  130.  
  131. /**
  132. * This method calculate the numbers of days for a given date of the year.
  133. *
  134. * @return the number of days till this date or between two days.
  135. */
  136. public int dayOfYear() {
  137. int dayOfYear = 0;
  138. for (int i = 0; i < month; i++) {
  139. dayOfYear = dayOfYear + getDaysInMonth(year, i);
  140. }
  141. dayOfYear = dayOfYear + day;
  142. return dayOfYear;
  143. }
  144.  
  145. /**
  146. * This method calculate how many days the other date is off.
  147. *
  148. * @param other
  149. * is the other date we need to make the difference.
  150. * @return the result in integer form.
  151. */
  152. public int sameYearDiff(Date other) {
  153. if (year == other.getYear()) {
  154. return other.dayOfYear() - dayOfYear();
  155. } else {
  156. return 0;
  157. }
  158.  
  159. }
  160.  
  161. /**
  162. * This is the method that return a date in a given format.
  163. * @return the date in a given format.
  164. */
  165. public String toString() {
  166.  
  167. return MONTHS[month - 1] + " " + day + " , " + year;
  168. }
  169.  
  170. /**
  171. * This is the method to test our program.
  172. *
  173. * @param args
  174. * we need to check the date.
  175. */
  176. public static void main(String[] args) {
  177. Date date = new Date(2015, 5, 20);
  178. System.out.println(date);
  179.  
  180. }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement