Advertisement
Guest User

ein ziehmlich nices programm

a guest
Dec 10th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. package programming.set6.date;
  2.  
  3. /**
  4. * A class that provides basic functions to handle dates.
  5. *
  6. * @author Bjarne
  7. */
  8. public class Date {
  9.  
  10. /**
  11. * the year of an instance of the class
  12. */
  13. private int year;
  14.  
  15. /**
  16. * the month of an instance of the class
  17. */
  18. private int month;
  19.  
  20. /**
  21. * the day of an instance of the class
  22. */
  23. private int day;
  24.  
  25. /**
  26. * An array that contains the number of days that each month has, where the
  27. * 0-th element represents January and the 11-th element represents
  28. * December.
  29. */
  30. private static final int[] DaysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31,
  31. 30, 31, 30, 31 };
  32.  
  33. /**
  34. * A second array that contains the number of days for each month within a
  35. * leap year.
  36. */
  37. private static final int[] DaysInMonth_LeapYear = { 31, 29, 31, 30, 31, 30,
  38. 31, 31, 30, 31, 30, 31 };
  39.  
  40. /**
  41. * An array that contains the names of the months.
  42. */
  43. private static final String[] MonthNames = { "January", "February",
  44. "March", "April", "May", "June", "July", "August", "September",
  45. "October", "November", "December" };
  46.  
  47. /**
  48. * Returns the number of days in a given month.
  49. *
  50. * @param year
  51. * the year is needed to check whether it is a leap year
  52. * @param month
  53. * the given month
  54. * @return days in the given month
  55. */
  56. public static int getDaysInMonth(int year, int month) {
  57.  
  58. // return 0 for an invalid month
  59.  
  60. if (month < 1 || month > 12) {
  61.  
  62. return 0;
  63. }
  64.  
  65. // check for leap year
  66.  
  67. if (year % 4 == 0 && year % 100 != 0 || year % 4 == 0
  68. && year % 100 == 0 && year % 400 == 0) {
  69.  
  70.  
  71. // If the year is a leap year, return the value from the leap year array.
  72.  
  73. // As January is the first month, but the 0-th element in the array,
  74. // we need to subtract 1 from month to get the correct value.
  75.  
  76.  
  77. return DaysInMonth_LeapYear[month - 1];
  78. }
  79.  
  80. // If the year isn't a leap year, use the default array.
  81.  
  82. else {
  83.  
  84. return DaysInMonth[month - 1];
  85. }
  86. }
  87.  
  88. /**
  89. * Returns true for a valid date and false for an invalid one.
  90. *
  91. * @param year
  92. * the year of the given date
  93. * @param month
  94. * the month of the given date
  95. * @param day
  96. * the day of the given date
  97. * @return boolean
  98. */
  99. public static boolean validate(int year, int month, int day) {
  100.  
  101. // check for valid month in the range from 1 - 12
  102. // and for a day greater 0.
  103.  
  104. if (month > 0 && month < 13 && day > 0) {
  105.  
  106. // If we have a leap year, compare the value of the given
  107. // day to the corresponding value in the leap year array.
  108.  
  109. if (year % 4 == 0 && year % 100 != 0 || year % 4 == 0
  110. && year % 100 == 0 && year % 400 == 0) {
  111.  
  112. if (day <= DaysInMonth_LeapYear[month - 1]) {
  113.  
  114. return true;
  115. }
  116.  
  117. // otherwise, use the default array
  118.  
  119. } else if (day <= DaysInMonth[month - 1]) {
  120.  
  121. return true;
  122. }
  123. }
  124.  
  125. // if the month is outside of the range 1 - 12, or the day is smaller than one,
  126. // or the value of the given day is greater than the number of days in the respective month,
  127. // return false.
  128.  
  129. return false;
  130. }
  131.  
  132. /**
  133. * To be able to declare further constructors, we need to declare this 0 -
  134. * constructor. (Would have been nice to know before, though)
  135. */
  136. public Date() {
  137. year = 0;
  138. month = 0;
  139. day = 0;
  140. }
  141.  
  142. /**
  143. * A constructor that initializes Date with the parameters year, month and
  144. * day. If the given values don't represent a valid date, an
  145. * IllegalArgumentException is thrown.
  146. *
  147. * @param year
  148. * the year of the date
  149. * @param month
  150. * the month of the date
  151. * @param day
  152. * the day of the date
  153. */
  154. public Date(int year, int month, int day) {
  155.  
  156. if (validate(year, month, day) == false) {
  157. throw new IllegalArgumentException("This is not a valid date.");
  158. }
  159.  
  160. this.year = year;
  161. this.month = month;
  162. this.day = day;
  163.  
  164. }
  165.  
  166. /**
  167. * Getter method to obtain the year of a date.
  168. *
  169. * @return year
  170. */
  171. public int getYear() {
  172. return this.year;
  173. }
  174.  
  175. /**
  176. * Getter method to obtain the month of a date.
  177. *
  178. * @return month
  179. */
  180. public int getMonth() {
  181. return this.month;
  182. }
  183.  
  184. /**
  185. * Getter method to obtain the day of a date.
  186. *
  187. * @return day
  188. */
  189. public int getDay() {
  190. return this.day;
  191. }
  192.  
  193. /**
  194. * Calculates the position of a given day within the given year
  195. *
  196. * @return position of the day
  197. */
  198. public int dayOfYear() {
  199.  
  200. // The variable n is used to sum up all days of the previous months.
  201.  
  202. int n = 0;
  203.  
  204. // as usual, if the year is a leap year, the leap year array is used.
  205.  
  206. if (year % 4 == 0 && year % 100 != 0 || year % 4 == 0
  207. && year % 100 == 0 && year % 400 == 0) {
  208.  
  209. for (int i = 0; i < this.month - 1; i++) {
  210. n += DaysInMonth_LeapYear[i];
  211. }
  212.  
  213. } else {
  214. for (int i = 0; i < this.month - 1; i++) {
  215. n += DaysInMonth[i];
  216. }
  217. }
  218.  
  219. // We add the days of our given date to n and thus get the position of the day within the year.
  220.  
  221. int DayOfYear = this.day + n;
  222. return DayOfYear;
  223. }
  224.  
  225. /**
  226. * Calculates the difference (in days) between two dates within the same
  227. * year
  228. *
  229. * @param other
  230. * the other date
  231. * @return difference of the positions
  232. */
  233. public int sameYearDiff(Date other) {
  234.  
  235. // The method will only work for dates with the same year
  236. // and otherwise return 0.
  237.  
  238. if (this.year - other.year != 0) {
  239. return 0;
  240. }
  241.  
  242. int diff = other.dayOfYear() - this.dayOfYear();
  243. return diff;
  244. }
  245.  
  246. /**
  247. * Creates a string that displays a given date. The array that contains the names
  248. * of the months is used here.
  249. */
  250. public String toString() {
  251.  
  252. String month = MonthNames[this.month - 1];
  253. String date = month + " " + this.day + ", " + this.year;
  254. return date;
  255.  
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement