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 Days extends BaseSingleFieldPeriod { 41 42 43 public static final Days ZERO = new Days(0); 44 45 public static final Days ONE = new Days(1); 46 47 public static final Days TWO = new Days(2); 48 49 public static final Days THREE = new Days(3); 50 51 public static final Days FOUR = new Days(4); 52 53 public static final Days FIVE = new Days(5); 54 55 public static final Days SIX = new Days(6); 56 57 public static final Days SEVEN = new Days(7); 58 59 public static final Days MAX_VALUE = new Days(Integer.MAX_VALUE); 60 61 public static final Days MIN_VALUE = new Days(Integer.MIN_VALUE); 62 63 64 private static final PeriodFormatter PARSER = ISOPeriodFormat.standard().withParseType(PeriodType.days()); 65 66 private static final long serialVersionUID = 87525275727380865L; 67 68 77 public static Days days(int days) { 78 switch (days) { 79 case 0: 80 return ZERO; 81 case 1: 82 return ONE; 83 case 2: 84 return TWO; 85 case 3: 86 return THREE; 87 case 4: 88 return FOUR; 89 case 5: 90 return FIVE; 91 case 6: 92 return SIX; 93 case 7: 94 return SEVEN; 95 case Integer.MAX_VALUE: 96 return MAX_VALUE; 97 case Integer.MIN_VALUE: 98 return MIN_VALUE; 99 default: 100 return new Days(days); 101 } 102 } 103 104 115 public static Days daysBetween(ReadableInstant start, ReadableInstant end) { 116 int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.days()); 117 return Days.days(amount); 118 } 119 120 132 public static Days daysBetween(ReadablePartial start, ReadablePartial end) { 133 if (start instanceof LocalDate && end instanceof LocalDate) { 134 Chronology chrono = DateTimeUtils.getChronology(start.getChronology()); 135 int days = chrono.days().getDifference( 136 ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis()); 137 return Days.days(days); 138 } 139 int amount = BaseSingleFieldPeriod.between(start, end, ZERO); 140 return Days.days(amount); 141 } 142 143 152 public static Days daysIn(ReadableInterval interval) { 153 if (interval == null) { 154 return Days.ZERO; 155 } 156 int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.days()); 157 return Days.days(amount); 158 } 159 160 180 public static Days standardDaysIn(ReadablePeriod period) { 181 int amount = BaseSingleFieldPeriod.standardPeriodIn(period, DateTimeConstants.MILLIS_PER_DAY); 182 return Days.days(amount); 183 } 184 185 196 public static Days parseDays(String periodStr) { 197 if (periodStr == null) { 198 return Days.ZERO; 199 } 200 Period p = PARSER.parsePeriod(periodStr); 201 return Days.days(p.getDays()); 202 } 203 204 212 private Days(int days) { 213 super(days); 214 } 215 216 221 private Object readResolve() { 222 return Days.days(getValue()); 223 } 224 225 231 public DurationFieldType getFieldType() { 232 return DurationFieldType.days(); 233 } 234 235 240 public PeriodType getPeriodType() { 241 return PeriodType.days(); 242 } 243 244 257 public Weeks toStandardWeeks() { 258 return Weeks.weeks(getValue() / DateTimeConstants.DAYS_PER_WEEK); 259 } 260 261 274 public Hours toStandardHours() { 275 return Hours.hours(FieldUtils.safeMultiply(getValue(), DateTimeConstants.HOURS_PER_DAY)); 276 } 277 278 292 public Minutes toStandardMinutes() { 293 return Minutes.minutes(FieldUtils.safeMultiply(getValue(), DateTimeConstants.MINUTES_PER_DAY)); 294 } 295 296 310 public Seconds toStandardSeconds() { 311 return Seconds.seconds(FieldUtils.safeMultiply(getValue(), DateTimeConstants.SECONDS_PER_DAY)); 312 } 313 314 328 public Duration toStandardDuration() { 329 long days = getValue(); return new Duration(days * DateTimeConstants.MILLIS_PER_DAY); 331 } 332 333 339 public int getDays() { 340 return getValue(); 341 } 342 343 353 public Days plus(int days) { 354 if (days == 0) { 355 return this; 356 } 357 return Days.days(FieldUtils.safeAdd(getValue(), days)); 358 } 359 360 369 public Days plus(Days days) { 370 if (days == null) { 371 return this; 372 } 373 return plus(days.getValue()); 374 } 375 376 386 public Days minus(int days) { 387 return plus(FieldUtils.safeNegate(days)); 388 } 389 390 399 public Days minus(Days days) { 400 if (days == null) { 401 return this; 402 } 403 return minus(days.getValue()); 404 } 405 406 416 public Days multipliedBy(int scalar) { 417 return Days.days(FieldUtils.safeMultiply(getValue(), scalar)); 418 } 419 420 430 public Days dividedBy(int divisor) { 431 if (divisor == 1) { 432 return this; 433 } 434 return Days.days(getValue() / divisor); 435 } 436 437 444 public Days negated() { 445 return Days.days(FieldUtils.safeNegate(getValue())); 446 } 447 448 455 public boolean isGreaterThan(Days other) { 456 if (other == null) { 457 return getValue() > 0; 458 } 459 return getValue() > other.getValue(); 460 } 461 462 468 public boolean isLessThan(Days other) { 469 if (other == null) { 470 return getValue() < 0; 471 } 472 return getValue() < other.getValue(); 473 } 474 475 483 public String toString() { 484 return "P" + String.valueOf(getValue()) + "D"; 485 } 486 487 } 488 | Popular Tags |