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