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 Hours extends BaseSingleFieldPeriod { 41 42 43 public static final Hours ZERO = new Hours(0); 44 45 public static final Hours ONE = new Hours(1); 46 47 public static final Hours TWO = new Hours(2); 48 49 public static final Hours THREE = new Hours(3); 50 51 public static final Hours FOUR = new Hours(4); 52 53 public static final Hours FIVE = new Hours(5); 54 55 public static final Hours SIX = new Hours(6); 56 57 public static final Hours SEVEN = new Hours(7); 58 59 public static final Hours EIGHT = new Hours(8); 60 61 public static final Hours MAX_VALUE = new Hours(Integer.MAX_VALUE); 62 63 public static final Hours MIN_VALUE = new Hours(Integer.MIN_VALUE); 64 65 66 private static final PeriodFormatter PARSER = ISOPeriodFormat.standard().withParseType(PeriodType.hours()); 67 68 private static final long serialVersionUID = 87525275727380864L; 69 70 79 public static Hours hours(int hours) { 80 switch (hours) { 81 case 0: 82 return ZERO; 83 case 1: 84 return ONE; 85 case 2: 86 return TWO; 87 case 3: 88 return THREE; 89 case 4: 90 return FOUR; 91 case 5: 92 return FIVE; 93 case 6: 94 return SIX; 95 case 7: 96 return SEVEN; 97 case 8: 98 return EIGHT; 99 case Integer.MAX_VALUE: 100 return MAX_VALUE; 101 case Integer.MIN_VALUE: 102 return MIN_VALUE; 103 default: 104 return new Hours(hours); 105 } 106 } 107 108 118 public static Hours hoursBetween(ReadableInstant start, ReadableInstant end) { 119 int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.hours()); 120 return Hours.hours(amount); 121 } 122 123 135 public static Hours hoursBetween(ReadablePartial start, ReadablePartial end) { 136 if (start instanceof LocalTime && end instanceof LocalTime) { 137 Chronology chrono = DateTimeUtils.getChronology(start.getChronology()); 138 int hours = chrono.hours().getDifference( 139 ((LocalTime) end).getLocalMillis(), ((LocalTime) start).getLocalMillis()); 140 return Hours.hours(hours); 141 } 142 int amount = BaseSingleFieldPeriod.between(start, end, ZERO); 143 return Hours.hours(amount); 144 } 145 146 154 public static Hours hoursIn(ReadableInterval interval) { 155 if (interval == null) { 156 return Hours.ZERO; 157 } 158 int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.hours()); 159 return Hours.hours(amount); 160 } 161 162 182 public static Hours standardHoursIn(ReadablePeriod period) { 183 int amount = BaseSingleFieldPeriod.standardPeriodIn(period, DateTimeConstants.MILLIS_PER_HOUR); 184 return Hours.hours(amount); 185 } 186 187 198 public static Hours parseHours(String periodStr) { 199 if (periodStr == null) { 200 return Hours.ZERO; 201 } 202 Period p = PARSER.parsePeriod(periodStr); 203 return Hours.hours(p.getHours()); 204 } 205 206 214 private Hours(int hours) { 215 super(hours); 216 } 217 218 223 private Object readResolve() { 224 return Hours.hours(getValue()); 225 } 226 227 233 public DurationFieldType getFieldType() { 234 return DurationFieldType.hours(); 235 } 236 237 242 public PeriodType getPeriodType() { 243 return PeriodType.hours(); 244 } 245 246 260 public Weeks toStandardWeeks() { 261 return Weeks.weeks(getValue() / DateTimeConstants.HOURS_PER_WEEK); 262 } 263 264 276 public Days toStandardDays() { 277 return Days.days(getValue() / DateTimeConstants.HOURS_PER_DAY); 278 } 279 280 292 public Minutes toStandardMinutes() { 293 return Minutes.minutes(FieldUtils.safeMultiply(getValue(), DateTimeConstants.MINUTES_PER_HOUR)); 294 } 295 296 309 public Seconds toStandardSeconds() { 310 return Seconds.seconds(FieldUtils.safeMultiply(getValue(), DateTimeConstants.SECONDS_PER_HOUR)); 311 } 312 313 327 public Duration toStandardDuration() { 328 long hours = getValue(); return new Duration(hours * DateTimeConstants.MILLIS_PER_HOUR); 330 } 331 332 338 public int getHours() { 339 return getValue(); 340 } 341 342 352 public Hours plus(int hours) { 353 if (hours == 0) { 354 return this; 355 } 356 return Hours.hours(FieldUtils.safeAdd(getValue(), hours)); 357 } 358 359 368 public Hours plus(Hours hours) { 369 if (hours == null) { 370 return this; 371 } 372 return plus(hours.getValue()); 373 } 374 375 385 public Hours minus(int hours) { 386 return plus(FieldUtils.safeNegate(hours)); 387 } 388 389 398 public Hours minus(Hours hours) { 399 if (hours == null) { 400 return this; 401 } 402 return minus(hours.getValue()); 403 } 404 405 415 public Hours multipliedBy(int scalar) { 416 return Hours.hours(FieldUtils.safeMultiply(getValue(), scalar)); 417 } 418 419 429 public Hours dividedBy(int divisor) { 430 if (divisor == 1) { 431 return this; 432 } 433 return Hours.hours(getValue() / divisor); 434 } 435 436 443 public Hours negated() { 444 return Hours.hours(FieldUtils.safeNegate(getValue())); 445 } 446 447 454 public boolean isGreaterThan(Hours other) { 455 if (other == null) { 456 return getValue() > 0; 457 } 458 return getValue() > other.getValue(); 459 } 460 461 467 public boolean isLessThan(Hours other) { 468 if (other == null) { 469 return getValue() < 0; 470 } 471 return getValue() < other.getValue(); 472 } 473 474 482 public String toString() { 483 return "PT" + String.valueOf(getValue()) + "H"; 484 } 485 486 } 487 | Popular Tags |