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 Months extends BaseSingleFieldPeriod { 41 42 43 public static final Months ZERO = new Months(0); 44 45 public static final Months ONE = new Months(1); 46 47 public static final Months TWO = new Months(2); 48 49 public static final Months THREE = new Months(3); 50 51 public static final Months FOUR = new Months(4); 52 53 public static final Months FIVE = new Months(5); 54 55 public static final Months SIX = new Months(6); 56 57 public static final Months SEVEN = new Months(7); 58 59 public static final Months EIGHT = new Months(8); 60 61 public static final Months NINE = new Months(9); 62 63 public static final Months TEN = new Months(10); 64 65 public static final Months ELEVEN = new Months(11); 66 67 public static final Months TWELVE = new Months(12); 68 69 public static final Months MAX_VALUE = new Months(Integer.MAX_VALUE); 70 71 public static final Months MIN_VALUE = new Months(Integer.MIN_VALUE); 72 73 74 private static final PeriodFormatter PARSER = ISOPeriodFormat.standard().withParseType(PeriodType.months()); 75 76 private static final long serialVersionUID = 87525275727380867L; 77 78 87 public static Months months(int months) { 88 switch (months) { 89 case 0: 90 return ZERO; 91 case 1: 92 return ONE; 93 case 2: 94 return TWO; 95 case 3: 96 return THREE; 97 case 4: 98 return FOUR; 99 case 5: 100 return FIVE; 101 case 6: 102 return SIX; 103 case 7: 104 return SEVEN; 105 case 8: 106 return EIGHT; 107 case 9: 108 return NINE; 109 case 10: 110 return TEN; 111 case 11: 112 return ELEVEN; 113 case 12: 114 return TWELVE; 115 case Integer.MAX_VALUE: 116 return MAX_VALUE; 117 case Integer.MIN_VALUE: 118 return MIN_VALUE; 119 default: 120 return new Months(months); 121 } 122 } 123 124 135 public static Months monthsBetween(ReadableInstant start, ReadableInstant end) { 136 int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.months()); 137 return Months.months(amount); 138 } 139 140 152 public static Months monthsBetween(ReadablePartial start, ReadablePartial end) { 153 if (start instanceof LocalDate && end instanceof LocalDate) { 154 Chronology chrono = DateTimeUtils.getChronology(start.getChronology()); 155 int months = chrono.months().getDifference( 156 ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis()); 157 return Months.months(months); 158 } 159 int amount = BaseSingleFieldPeriod.between(start, end, ZERO); 160 return Months.months(amount); 161 } 162 163 172 public static Months monthsIn(ReadableInterval interval) { 173 if (interval == null) { 174 return Months.ZERO; 175 } 176 int amount = BaseSingleFieldPeriod.between(interval.getStart(), interval.getEnd(), DurationFieldType.months()); 177 return Months.months(amount); 178 } 179 180 191 public static Months parseMonths(String periodStr) { 192 if (periodStr == null) { 193 return Months.ZERO; 194 } 195 Period p = PARSER.parsePeriod(periodStr); 196 return Months.months(p.getMonths()); 197 } 198 199 207 private Months(int months) { 208 super(months); 209 } 210 211 216 private Object readResolve() { 217 return Months.months(getValue()); 218 } 219 220 226 public DurationFieldType getFieldType() { 227 return DurationFieldType.months(); 228 } 229 230 235 public PeriodType getPeriodType() { 236 return PeriodType.months(); 237 } 238 239 245 public int getMonths() { 246 return getValue(); 247 } 248 249 259 public Months plus(int months) { 260 if (months == 0) { 261 return this; 262 } 263 return Months.months(FieldUtils.safeAdd(getValue(), months)); 264 } 265 266 275 public Months plus(Months months) { 276 if (months == null) { 277 return this; 278 } 279 return plus(months.getValue()); 280 } 281 282 292 public Months minus(int months) { 293 return plus(FieldUtils.safeNegate(months)); 294 } 295 296 305 public Months minus(Months months) { 306 if (months == null) { 307 return this; 308 } 309 return minus(months.getValue()); 310 } 311 312 322 public Months multipliedBy(int scalar) { 323 return Months.months(FieldUtils.safeMultiply(getValue(), scalar)); 324 } 325 326 336 public Months dividedBy(int divisor) { 337 if (divisor == 1) { 338 return this; 339 } 340 return Months.months(getValue() / divisor); 341 } 342 343 350 public Months negated() { 351 return Months.months(FieldUtils.safeNegate(getValue())); 352 } 353 354 361 public boolean isGreaterThan(Months other) { 362 if (other == null) { 363 return getValue() > 0; 364 } 365 return getValue() > other.getValue(); 366 } 367 368 374 public boolean isLessThan(Months other) { 375 if (other == null) { 376 return getValue() < 0; 377 } 378 return getValue() < other.getValue(); 379 } 380 381 389 public String toString() { 390 return "P" + String.valueOf(getValue()) + "M"; 391 } 392 393 } 394 | Popular Tags |