Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. package sk.stuba.fei.Zadanie2cvikouloha1;
  2.  
  3. public class MyDate {
  4. public int getDay() {
  5. return day;
  6. }
  7.  
  8. public void setDay(int day) {
  9. this.day = day;
  10. }
  11.  
  12. public int getMonth() {
  13. return month;
  14. }
  15.  
  16. public void setMonth(int month) {
  17. this.month = month;
  18. }
  19.  
  20. public int getYear() {
  21. return year;
  22. }
  23.  
  24. public void setYear(int year) {
  25. this.year = year;
  26. }
  27.  
  28. public String[] getStrMonthsSk() {
  29. return strMonthsSk;
  30. }
  31.  
  32. public void setStrMonthsSk(String[] strMonthsSk) {
  33. this.strMonthsSk = strMonthsSk;
  34. }
  35.  
  36. public String[] getStrDaysSk() {
  37. return strDaysSk;
  38. }
  39.  
  40. public void setStrDaysSk(String[] strDaysSk) {
  41. this.strDaysSk = strDaysSk;
  42. }
  43.  
  44. public int[] getDaysInMonths() {
  45. return daysInMonths;
  46. }
  47.  
  48. public void setDaysInMonths(int[] daysInMonths) {
  49. this.daysInMonths = daysInMonths;
  50. }
  51.  
  52. private int day;
  53. private int month;
  54. private int year;
  55. private static String[] strMonthsSk = {"Jan", "Feb", "Mar", "Apr", "Maj", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"};
  56. private static String[] strDaysSk = {"Po", "Ut", "Str", "Stv", "Pia", "So", "Ne"};
  57. private static int[] daysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  58.  
  59. public MyDate(int day, int month, int year) {
  60. this.day = day;
  61. this.month = month;
  62. this.year = year;
  63. }
  64.  
  65.  
  66. public void setDate(int day, int month, int year) {
  67. this.day = day;
  68. this.month = month;
  69. this.year = year;
  70. }
  71.  
  72.  
  73. public static boolean isLeapYear(int year) {
  74. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true;
  75. return false;
  76. }
  77.  
  78.  
  79. public static boolean isValidDate(int day, int month, int year) {
  80. if (year >= 1 && year <= 9999) {
  81. if (month >= 1 && month <= 12) {
  82.  
  83. if (month == 2) {
  84. if (isLeapYear(year)) {
  85. if (day >= 1 && day <= 29) return true;
  86. else return false;
  87. } else if (day >= 1 && day <= 28) return true;
  88. else return false;
  89.  
  90. } else {
  91. if (day >= 1 && day <= daysInMonths[month - 1]) {
  92. return true;
  93. } else return false;
  94. }
  95.  
  96. } else return false;
  97. } else return false;
  98. }
  99.  
  100. public MyDate nextDay(int day, int month, int year) {
  101. if (month == 12 && day == 31) {
  102. this.year += 1;
  103. this.month = 1;
  104.  
  105. } else if (month >= 1 && month < 12) {
  106.  
  107. if (month == 2) {
  108. if (isLeapYear(year)) {
  109. if (day >= 1 && day < 29) this.day++;
  110. else {
  111. this.day = 1;
  112. this.month++;
  113. }
  114. } else if (day >= 1 && day < 28) this.day++;
  115. else {
  116. this.day = 1;
  117. this.month++;
  118. }
  119. } else {
  120. if (day >= 1 && day < daysInMonths[month - 1]) {
  121. this.day++;
  122. } else this.day = 1;
  123. this.month++;
  124. }
  125.  
  126. }
  127. return this;
  128. }
  129.  
  130.  
  131. public MyDate nextYear() {
  132. if (year < 9999) {
  133. year++;
  134. }
  135. return this;
  136. }
  137.  
  138. public static int getDayOfWeek(int d, int m, int y) {
  139. int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
  140. y -= (m < 3) ? 1 : 0;
  141. return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
  142. }
  143. public static void nameOfDay(int d)
  144. {
  145. switch (d){
  146. case 1:
  147. System.out.println(strDaysSk[0]);break;
  148. case 2:
  149. System.out.println(strDaysSk[1]);break;
  150. case 3:
  151. System.out.println(strDaysSk[2]);break;
  152. case 4:
  153. System.out.println(strDaysSk[3]);break;
  154. case 5:
  155. System.out.println(strDaysSk[4]);break;
  156. case 6:
  157. System.out.println(strDaysSk[5]);break;
  158. case 0:
  159. System.out.println(strDaysSk[6]);break;
  160. }
  161. }
  162.  
  163. @Override
  164. public String toString() {
  165. return
  166. "day=" + day +
  167. ", month=" + month +
  168. ", year=" + year
  169. ;
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement