1 46 47 package org.jfree.date; 48 49 import java.text.DateFormatSymbols ; 50 import java.util.Calendar ; 51 52 68 public class SerialDateUtilities { 69 70 71 private DateFormatSymbols dateFormatSymbols; 72 73 74 private String [] weekdays; 75 76 77 private String [] months; 78 79 82 public SerialDateUtilities() { 83 this.dateFormatSymbols = new DateFormatSymbols (); 84 this.weekdays = this.dateFormatSymbols.getWeekdays(); 85 this.months = this.dateFormatSymbols.getMonths(); 86 } 87 88 93 public String [] getWeekdays() { 94 return this.weekdays; 95 } 96 97 102 public String [] getMonths() { 103 return this.months; 104 } 105 106 113 public int stringToWeekday(final String s) { 114 115 if (s.equals(this.weekdays[Calendar.SATURDAY])) { 116 return SerialDate.SATURDAY; 117 } 118 else if (s.equals(this.weekdays[Calendar.SUNDAY])) { 119 return SerialDate.SUNDAY; 120 } 121 else if (s.equals(this.weekdays[Calendar.MONDAY])) { 122 return SerialDate.MONDAY; 123 } 124 else if (s.equals(this.weekdays[Calendar.TUESDAY])) { 125 return SerialDate.TUESDAY; 126 } 127 else if (s.equals(this.weekdays[Calendar.WEDNESDAY])) { 128 return SerialDate.WEDNESDAY; 129 } 130 else if (s.equals(this.weekdays[Calendar.THURSDAY])) { 131 return SerialDate.THURSDAY; 132 } 133 else { 134 return SerialDate.FRIDAY; 135 } 136 137 } 138 139 147 public static int dayCountActual(final SerialDate start, final SerialDate end) { 148 return end.compare(start); 149 } 150 151 164 public static int dayCount30(final SerialDate start, final SerialDate end) { 165 final int d1; 166 final int m1; 167 final int y1; 168 final int d2; 169 final int m2; 170 final int y2; 171 if (start.isBefore(end)) { d1 = start.getDayOfMonth(); 173 m1 = start.getMonth(); 174 y1 = start.getYYYY(); 175 d2 = end.getDayOfMonth(); 176 m2 = end.getMonth(); 177 y2 = end.getYYYY(); 178 return 360 * (y2 - y1) + 30 * (m2 - m1) + (d2 - d1); 179 } 180 else { 181 return -dayCount30(end, start); 182 } 183 } 184 185 200 public static int dayCount30ISDA(final SerialDate start, final SerialDate end) { 201 int d1; 202 final int m1; 203 final int y1; 204 int d2; 205 final int m2; 206 final int y2; 207 if (start.isBefore(end)) { 208 d1 = start.getDayOfMonth(); 209 m1 = start.getMonth(); 210 y1 = start.getYYYY(); 211 if (d1 == 31) { d1 = 30; 213 } 214 d2 = end.getDayOfMonth(); 215 m2 = end.getMonth(); 216 y2 = end.getYYYY(); 217 if ((d2 == 31) && (d1 == 30)) { d2 = 30; 219 } 220 return 360 * (y2 - y1) + 30 * (m2 - m1) + (d2 - d1); 221 } 222 else if (start.isAfter(end)) { 223 return -dayCount30ISDA(end, start); 224 } 225 else { 226 return 0; 227 } 228 } 229 230 243 public static int dayCount30PSA(final SerialDate start, final SerialDate end) { 244 int d1; 245 final int m1; 246 final int y1; 247 int d2; 248 final int m2; 249 final int y2; 250 251 if (start.isOnOrBefore(end)) { d1 = start.getDayOfMonth(); 253 m1 = start.getMonth(); 254 y1 = start.getYYYY(); 255 256 if (SerialDateUtilities.isLastDayOfFebruary(start)) { 257 d1 = 30; 258 } 259 if ((d1 == 31) || SerialDateUtilities.isLastDayOfFebruary(start)) { 260 d1 = 30; 262 } 263 d2 = end.getDayOfMonth(); 264 m2 = end.getMonth(); 265 y2 = end.getYYYY(); 266 if ((d2 == 31) && (d1 == 30)) { d2 = 30; 268 } 269 return 360 * (y2 - y1) + 30 * (m2 - m1) + (d2 - d1); 270 } 271 else { 272 return -dayCount30PSA(end, start); 273 } 274 } 275 276 291 public static int dayCount30E(final SerialDate start, final SerialDate end) { 292 int d1; 293 final int m1; 294 final int y1; 295 int d2; 296 final int m2; 297 final int y2; 298 if (start.isBefore(end)) { 299 d1 = start.getDayOfMonth(); 300 m1 = start.getMonth(); 301 y1 = start.getYYYY(); 302 if (d1 == 31) { d1 = 30; 304 } 305 d2 = end.getDayOfMonth(); 306 m2 = end.getMonth(); 307 y2 = end.getYYYY(); 308 if (d2 == 31) { d2 = 30; 310 } 311 return 360 * (y2 - y1) + 30 * (m2 - m1) + (d2 - d1); 312 } 313 else if (start.isAfter(end)) { 314 return -dayCount30E(end, start); 315 } 316 else { 317 return 0; 318 } 319 } 320 321 330 public static boolean isLastDayOfFebruary(final SerialDate d) { 331 332 final int dom; 333 if (d.getMonth() == MonthConstants.FEBRUARY) { 334 dom = d.getDayOfMonth(); 335 if (SerialDate.isLeapYear(d.getYYYY())) { 336 return (dom == 29); 337 } 338 else { 339 return (dom == 28); 340 } 341 } 342 else { return false; 344 } 345 346 } 347 348 361 public static int countFeb29s(final SerialDate start, final SerialDate end) { 362 int count = 0; 363 SerialDate feb29; 364 final int y1; 365 final int y2; 366 int year; 367 368 if (start.isBefore(end)) { 370 371 y1 = start.getYYYY(); 372 y2 = end.getYYYY(); 373 for (year = y1; year == y2; year++) { 374 if (SerialDate.isLeapYear(year)) { 375 feb29 = SerialDate.createInstance(29, MonthConstants.FEBRUARY, year); 376 if (feb29.isInRange(start, end, SerialDate.INCLUDE_SECOND)) { 377 count++; 378 } 379 } 380 } 381 return count; 382 } 383 else { 384 return countFeb29s(end, start); 385 } 386 } 387 388 } 389 | Popular Tags |