1 16 package org.joda.time; 17 18 import org.joda.time.base.BaseSingleFieldPeriod; 19 import org.joda.time.field.FieldUtils; 20 import org.joda.time.format.ISOPeriodFormat; 21 import org.joda.time.format.PeriodFormatter; 22 23 40 public final class Weeks extends BaseSingleFieldPeriod { 41 42 43 public static final Weeks ZERO = new Weeks(0); 44 45 public static final Weeks ONE = new Weeks(1); 46 47 public static final Weeks TWO = new Weeks(2); 48 49 public static final Weeks THREE = new Weeks(3); 50 51 public static final Weeks MAX_VALUE = new Weeks(Integer.MAX_VALUE); 52 53 public static final Weeks MIN_VALUE = new Weeks(Integer.MIN_VALUE); 54 55 56 private static final PeriodFormatter PARSER = ISOPeriodFormat.standard().withParseType(PeriodType.weeks()); 57 58 private static final long serialVersionUID = 87525275727380866L; 59 60 69 public static Weeks weeks(int weeks) { 70 switch (weeks) { 71 case 0: 72 return ZERO; 73 case 1: 74 return ONE; 75 case 2: 76 return TWO; 77 case 3: 78 return THREE; 79 case Integer.MAX_VALUE: 80 return MAX_VALUE; 81 case Integer.MIN_VALUE: 82 return MIN_VALUE; 83 default: 84 return new Weeks(weeks); 85 } 86 } 87 88 98 public static Weeks weeksBetween(ReadableInstant start, ReadableInstant end) { 99 int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.weeks()); 100 return Weeks.weeks(amount); 101 } 102 103 115 public static Weeks weeksBetween(ReadablePartial start, ReadablePartial end) { 116 if (start instanceof LocalDate && end instanceof LocalDate) { 117 Chronology chrono = DateTimeUtils.getChronology(start.getChronology()); 118 int weeks = chrono.weeks().getDifference( 119 ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis()); 120 return Weeks.weeks(weeks); 121 } 122 int amount = BaseSingleFieldPeriod.between(start, end, ZERO); 123 return Weeks.weeks(amount); 124 } 125 126 134 public static Weeks weeksIn(ReadableInterval interval) { 135 if (interval == null) { 136 return Weeks.ZERO; 137 } 138 int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.weeks()); 139 return Weeks.weeks(amount); 140 } 141 142 162 public static Weeks standardWeeksIn(ReadablePeriod period) { 163 int amount = BaseSingleFieldPeriod.standardPeriodIn(period, DateTimeConstants.MILLIS_PER_WEEK); 164 return Weeks.weeks(amount); 165 } 166 167 178 public static Weeks parseWeeks(String periodStr) { 179 if (periodStr == null) { 180 return Weeks.ZERO; 181 } 182 Period p = PARSER.parsePeriod(periodStr); 183 return Weeks.weeks(p.getWeeks()); 184 } 185 186 194 private Weeks(int weeks) { 195 super(weeks); 196 } 197 198 203 private Object readResolve() { 204 return Weeks.weeks(getValue()); 205 } 206 207 213 public DurationFieldType getFieldType() { 214 return DurationFieldType.weeks(); 215 } 216 217 222 public PeriodType getPeriodType() { 223 return PeriodType.weeks(); 224 } 225 226 240 public Days toStandardDays() { 241 return Days.days(FieldUtils.safeMultiply(getValue(), DateTimeConstants.DAYS_PER_WEEK)); 242 } 243 244 258 public Hours toStandardHours() { 259 return Hours.hours(FieldUtils.safeMultiply(getValue(), DateTimeConstants.HOURS_PER_WEEK)); 260 } 261 262 276 public Minutes toStandardMinutes() { 277 return Minutes.minutes(FieldUtils.safeMultiply(getValue(), DateTimeConstants.MINUTES_PER_WEEK)); 278 } 279 280 295 public Seconds toStandardSeconds() { 296 return Seconds.seconds(FieldUtils.safeMultiply(getValue(), DateTimeConstants.SECONDS_PER_WEEK)); 297 } 298 299 314 public Duration toStandardDuration() { 315 long weeks = getValue(); return new Duration(weeks * DateTimeConstants.MILLIS_PER_WEEK); 317 } 318 319 325 public int getWeeks() { 326 return getValue(); 327 } 328 329 339 public Weeks plus(int weeks) { 340 if (weeks == 0) { 341 return this; 342 } 343 return Weeks.weeks(FieldUtils.safeAdd(getValue(), weeks)); 344 } 345 346 355 public Weeks plus(Weeks weeks) { 356 if (weeks == null) { 357 return this; 358 } 359 return plus(weeks.getValue()); 360 } 361 362 372 public Weeks minus(int weeks) { 373 return plus(FieldUtils.safeNegate(weeks)); 374 } 375 376 385 public Weeks minus(Weeks weeks) { 386 if (weeks == null) { 387 return this; 388 } 389 return minus(weeks.getValue()); 390 } 391 392 402 public Weeks multipliedBy(int scalar) { 403 return Weeks.weeks(FieldUtils.safeMultiply(getValue(), scalar)); 404 } 405 406 416 public Weeks dividedBy(int divisor) { 417 if (divisor == 1) { 418 return this; 419 } 420 return Weeks.weeks(getValue() / divisor); 421 } 422 423 430 public Weeks negated() { 431 return Weeks.weeks(FieldUtils.safeNegate(getValue())); 432 } 433 434 441 public boolean isGreaterThan(Weeks other) { 442 if (other == null) { 443 return getValue() > 0; 444 } 445 return getValue() > other.getValue(); 446 } 447 448 454 public boolean isLessThan(Weeks other) { 455 if (other == null) { 456 return getValue() < 0; 457 } 458 return getValue() < other.getValue(); 459 } 460 461 469 public String toString() { 470 return "P" + String.valueOf(getValue()) + "W"; 471 } 472 473 } 474 | Popular Tags |