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 Minutes extends BaseSingleFieldPeriod { 41 42 43 public static final Minutes ZERO = new Minutes(0); 44 45 public static final Minutes ONE = new Minutes(1); 46 47 public static final Minutes TWO = new Minutes(2); 48 49 public static final Minutes THREE = new Minutes(3); 50 51 public static final Minutes MAX_VALUE = new Minutes(Integer.MAX_VALUE); 52 53 public static final Minutes MIN_VALUE = new Minutes(Integer.MIN_VALUE); 54 55 56 private static final PeriodFormatter PARSER = ISOPeriodFormat.standard().withParseType(PeriodType.minutes()); 57 58 private static final long serialVersionUID = 87525275727380863L; 59 60 69 public static Minutes minutes(int minutes) { 70 switch (minutes) { 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 Minutes(minutes); 85 } 86 } 87 88 98 public static Minutes minutesBetween(ReadableInstant start, ReadableInstant end) { 99 int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.minutes()); 100 return Minutes.minutes(amount); 101 } 102 103 115 public static Minutes minutesBetween(ReadablePartial start, ReadablePartial end) { 116 if (start instanceof LocalTime && end instanceof LocalTime) { 117 Chronology chrono = DateTimeUtils.getChronology(start.getChronology()); 118 int minutes = chrono.minutes().getDifference( 119 ((LocalTime) end).getLocalMillis(), ((LocalTime) start).getLocalMillis()); 120 return Minutes.minutes(minutes); 121 } 122 int amount = BaseSingleFieldPeriod.between(start, end, ZERO); 123 return Minutes.minutes(amount); 124 } 125 126 134 public static Minutes minutesIn(ReadableInterval interval) { 135 if (interval == null) { 136 return Minutes.ZERO; 137 } 138 int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.minutes()); 139 return Minutes.minutes(amount); 140 } 141 142 162 public static Minutes standardMinutesIn(ReadablePeriod period) { 163 int amount = BaseSingleFieldPeriod.standardPeriodIn(period, DateTimeConstants.MILLIS_PER_MINUTE); 164 return Minutes.minutes(amount); 165 } 166 167 178 public static Minutes parseMinutes(String periodStr) { 179 if (periodStr == null) { 180 return Minutes.ZERO; 181 } 182 Period p = PARSER.parsePeriod(periodStr); 183 return Minutes.minutes(p.getMinutes()); 184 } 185 186 194 private Minutes(int minutes) { 195 super(minutes); 196 } 197 198 203 private Object readResolve() { 204 return Minutes.minutes(getValue()); 205 } 206 207 213 public DurationFieldType getFieldType() { 214 return DurationFieldType.minutes(); 215 } 216 217 222 public PeriodType getPeriodType() { 223 return PeriodType.minutes(); 224 } 225 226 240 public Weeks toStandardWeeks() { 241 return Weeks.weeks(getValue() / DateTimeConstants.MINUTES_PER_WEEK); 242 } 243 244 257 public Days toStandardDays() { 258 return Days.days(getValue() / DateTimeConstants.MINUTES_PER_DAY); 259 } 260 261 273 public Hours toStandardHours() { 274 return Hours.hours(getValue() / DateTimeConstants.MINUTES_PER_HOUR); 275 } 276 277 290 public Seconds toStandardSeconds() { 291 return Seconds.seconds(FieldUtils.safeMultiply(getValue(), DateTimeConstants.SECONDS_PER_MINUTE)); 292 } 293 294 308 public Duration toStandardDuration() { 309 long minutes = getValue(); return new Duration(minutes * DateTimeConstants.MILLIS_PER_MINUTE); 311 } 312 313 319 public int getMinutes() { 320 return getValue(); 321 } 322 323 333 public Minutes plus(int minutes) { 334 if (minutes == 0) { 335 return this; 336 } 337 return Minutes.minutes(FieldUtils.safeAdd(getValue(), minutes)); 338 } 339 340 349 public Minutes plus(Minutes minutes) { 350 if (minutes == null) { 351 return this; 352 } 353 return plus(minutes.getValue()); 354 } 355 356 366 public Minutes minus(int minutes) { 367 return plus(FieldUtils.safeNegate(minutes)); 368 } 369 370 379 public Minutes minus(Minutes minutes) { 380 if (minutes == null) { 381 return this; 382 } 383 return minus(minutes.getValue()); 384 } 385 386 396 public Minutes multipliedBy(int scalar) { 397 return Minutes.minutes(FieldUtils.safeMultiply(getValue(), scalar)); 398 } 399 400 410 public Minutes dividedBy(int divisor) { 411 if (divisor == 1) { 412 return this; 413 } 414 return Minutes.minutes(getValue() / divisor); 415 } 416 417 424 public Minutes negated() { 425 return Minutes.minutes(FieldUtils.safeNegate(getValue())); 426 } 427 428 435 public boolean isGreaterThan(Minutes other) { 436 if (other == null) { 437 return getValue() > 0; 438 } 439 return getValue() > other.getValue(); 440 } 441 442 448 public boolean isLessThan(Minutes other) { 449 if (other == null) { 450 return getValue() < 0; 451 } 452 return getValue() < other.getValue(); 453 } 454 455 463 public String toString() { 464 return "PT" + String.valueOf(getValue()) + "M"; 465 } 466 467 } 468 | Popular Tags |