1 63 64 package org.jfree.data.time; 65 66 import java.io.Serializable ; 67 import java.util.Calendar ; 68 import java.util.Date ; 69 import java.util.TimeZone ; 70 71 81 public class Week extends RegularTimePeriod implements Serializable { 82 83 84 private static final long serialVersionUID = 1856387786939865061L; 85 86 87 public static final int FIRST_WEEK_IN_YEAR = 1; 88 89 90 public static final int LAST_WEEK_IN_YEAR = 53; 91 92 93 private Year year; 94 95 96 private int week; 97 98 102 public Week() { 103 this(new Date ()); 104 } 105 106 112 public Week(int week, int year) { 113 this(week, new Year(year)); 114 } 115 116 122 public Week(int week, Year year) { 123 if ((week < FIRST_WEEK_IN_YEAR) && (week > LAST_WEEK_IN_YEAR)) { 124 throw new IllegalArgumentException ( 125 "The 'week' argument must be in the range 1 - 53." 126 ); 127 } 128 this.week = week; 129 this.year = year; 130 } 131 132 138 public Week(Date time) { 139 this(time, RegularTimePeriod.DEFAULT_TIME_ZONE); 141 } 142 143 150 public Week(Date time, TimeZone zone) { 151 if (time == null) { 152 throw new IllegalArgumentException ("Null 'time' argument."); 153 } 154 if (zone == null) { 155 throw new IllegalArgumentException ("Null 'zone' argument."); 156 } 157 Calendar calendar = Calendar.getInstance(zone); 158 calendar.setTime(time); 159 160 int tempWeek = calendar.get(Calendar.WEEK_OF_YEAR); 164 if (tempWeek == 1 165 && calendar.get(Calendar.MONTH) == Calendar.DECEMBER) { 166 this.week = 1; 167 this.year = new Year(calendar.get(Calendar.YEAR) + 1); 168 } 169 else { 170 this.week = Math.min(tempWeek, LAST_WEEK_IN_YEAR); 171 this.year = new Year(calendar.get(Calendar.YEAR)); 172 } 173 174 } 175 176 181 public Year getYear() { 182 return this.year; 183 } 184 185 190 public int getYearValue() { 191 return this.year.getYear(); 192 } 193 194 199 public int getWeek() { 200 return this.week; 201 } 202 203 211 public RegularTimePeriod previous() { 212 213 Week result; 214 if (this.week != FIRST_WEEK_IN_YEAR) { 215 result = new Week(this.week - 1, this.year); 216 } 217 else { 218 Year prevYear = (Year) this.year.previous(); 220 if (prevYear != null) { 221 int yy = prevYear.getYear(); 222 Calendar prevYearCalendar = Calendar.getInstance(); 223 prevYearCalendar.set(yy, Calendar.DECEMBER, 31); 224 result = new Week( 225 prevYearCalendar.getActualMaximum(Calendar.WEEK_OF_YEAR), 226 prevYear 227 ); 228 } 229 else { 230 result = null; 231 } 232 } 233 return result; 234 235 } 236 237 246 public RegularTimePeriod next() { 247 248 Week result; 249 if (this.week < 52) { 250 result = new Week(this.week + 1, this.year); 251 } 252 else { 253 Calendar calendar = Calendar.getInstance(); 254 calendar.set(this.year.getYear(), Calendar.DECEMBER, 31); 255 int actualMaxWeek 256 = calendar.getActualMaximum(Calendar.WEEK_OF_YEAR); 257 if (this.week != actualMaxWeek) { 258 result = new Week(this.week + 1, this.year); 259 } 260 else { 261 Year nextYear = (Year) this.year.next(); 262 if (nextYear != null) { 263 result = new Week(FIRST_WEEK_IN_YEAR, nextYear); 264 } 265 else { 266 result = null; 267 } 268 } 269 } 270 return result; 271 272 } 273 274 279 public long getSerialIndex() { 280 return this.year.getYear() * 53L + this.week; 281 } 282 283 291 public long getFirstMillisecond(Calendar calendar) { 292 Calendar c = (Calendar ) calendar.clone(); 293 c.clear(); 294 c.set(Calendar.YEAR, this.year.getYear()); 295 c.set(Calendar.WEEK_OF_YEAR, this.week); 296 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); 297 c.set(Calendar.HOUR, 0); 298 c.set(Calendar.MINUTE, 0); 299 c.set(Calendar.SECOND, 0); 300 c.set(Calendar.MILLISECOND, 0); 301 return c.getTime().getTime(); 303 } 304 305 313 public long getLastMillisecond(Calendar calendar) { 314 RegularTimePeriod next = next(); 315 return next.getFirstMillisecond(calendar) - 1; 316 } 317 318 325 public String toString() { 326 return "Week " + this.week + ", " + this.year; 327 } 328 329 338 public boolean equals(Object obj) { 339 340 if (obj == this) { 341 return true; 342 } 343 if (!(obj instanceof Week)) { 344 return false; 345 } 346 Week that = (Week) obj; 347 if (this.week != that.week) { 348 return false; 349 } 350 if (!this.year.equals(that.year)) { 351 return false; 352 } 353 return true; 354 355 } 356 357 366 public int hashCode() { 367 int result = 17; 368 result = 37 * result + this.week; 369 result = 37 * result + this.year.hashCode(); 370 return result; 371 } 372 373 383 public int compareTo(Object o1) { 384 385 int result; 386 387 if (o1 instanceof Week) { 390 Week w = (Week) o1; 391 result = this.year.getYear() - w.getYear().getYear(); 392 if (result == 0) { 393 result = this.week - w.getWeek(); 394 } 395 } 396 397 else if (o1 instanceof RegularTimePeriod) { 400 result = 0; 402 } 403 404 else { 407 result = 1; 409 } 410 411 return result; 412 413 } 414 415 426 public static Week parseWeek(String s) { 427 428 Week result = null; 429 if (s != null) { 430 431 s = s.trim(); 433 434 int i = Week.findSeparator(s); 435 if (i != -1) { 436 String s1 = s.substring(0, i).trim(); 437 String s2 = s.substring(i + 1, s.length()).trim(); 438 439 Year y = Week.evaluateAsYear(s1); 440 int w; 441 if (y != null) { 442 w = Week.stringToWeek(s2); 443 if (w == -1) { 444 throw new TimePeriodFormatException( 445 "Can't evaluate the week." 446 ); 447 } 448 result = new Week(w, y); 449 } 450 else { 451 y = Week.evaluateAsYear(s2); 452 if (y != null) { 453 w = Week.stringToWeek(s1); 454 if (w == -1) { 455 throw new TimePeriodFormatException( 456 "Can't evaluate the week." 457 ); 458 } 459 result = new Week(w, y); 460 } 461 else { 462 throw new TimePeriodFormatException( 463 "Can't evaluate the year." 464 ); 465 } 466 } 467 468 } 469 else { 470 throw new TimePeriodFormatException( 471 "Could not find separator." 472 ); 473 } 474 475 } 476 return result; 477 478 } 479 480 488 private static int findSeparator(String s) { 489 490 int result = s.indexOf('-'); 491 if (result == -1) { 492 result = s.indexOf(','); 493 } 494 if (result == -1) { 495 result = s.indexOf(' '); 496 } 497 if (result == -1) { 498 result = s.indexOf('.'); 499 } 500 return result; 501 } 502 503 512 private static Year evaluateAsYear(String s) { 513 514 Year result = null; 515 try { 516 result = Year.parseYear(s); 517 } 518 catch (TimePeriodFormatException e) { 519 } 521 return result; 522 523 } 524 525 532 private static int stringToWeek(String s) { 533 534 int result = -1; 535 s = s.replace('W', ' '); 536 s = s.trim(); 537 try { 538 result = Integer.parseInt(s); 539 if ((result < 1) || (result > LAST_WEEK_IN_YEAR)) { 540 result = -1; 541 } 542 } 543 catch (NumberFormatException e) { 544 } 546 return result; 547 548 } 549 550 } 551 | Popular Tags |