예제 #1
0
 /**
  * Creates a <code>Years</code> representing the number of whole years in the specified interval.
  * This method corectly handles any daylight savings time changes that may occur during the
  * interval.
  *
  * @param interval the interval to extract years from, null returns zero
  * @return the period in years
  * @throws IllegalArgumentException if the partials are null or invalid
  */
 public static Years yearsIn(ReadableInterval interval) {
   if (interval == null) {
     return Years.ZERO;
   }
   int amount =
       BaseSingleFieldPeriod.between(
           interval.getStart(), interval.getEnd(), DurationFieldType.years());
   return Years.years(amount);
 }
예제 #2
0
 /**
  * Gets a type that defines just the years field.
  *
  * @return the period type
  */
 public static PeriodType years() {
   PeriodType type = cYears;
   if (type == null) {
     type =
         new PeriodType(
             "Years",
             new DurationFieldType[] {DurationFieldType.years()},
             new int[] {
               0, -1, -1, -1, -1, -1, -1, -1,
             });
     cYears = type;
   }
   return type;
 }
예제 #3
0
 /**
  * Gets a type that defines the year and day fields.
  *
  * <ul>
  *   <li>years
  *   <li>days
  * </ul>
  *
  * @return the period type
  * @since 1.1
  */
 public static PeriodType yearDay() {
   PeriodType type = cYD;
   if (type == null) {
     type =
         new PeriodType(
             "YearDay",
             new DurationFieldType[] {
               DurationFieldType.years(), DurationFieldType.days(),
             },
             new int[] {
               0, -1, -1, 1, -1, -1, -1, -1,
             });
     cYD = type;
   }
   return type;
 }
예제 #4
0
 /**
  * Gets a type that defines all standard fields except months and weeks.
  *
  * <ul>
  *   <li>years
  *   <li>days
  *   <li>hours
  *   <li>minutes
  *   <li>seconds
  *   <li>milliseconds
  * </ul>
  *
  * @return the period type
  */
 public static PeriodType yearDayTime() {
   PeriodType type = cYDTime;
   if (type == null) {
     type =
         new PeriodType(
             "YearDayTime",
             new DurationFieldType[] {
               DurationFieldType.years(), DurationFieldType.days(),
               DurationFieldType.hours(), DurationFieldType.minutes(),
               DurationFieldType.seconds(), DurationFieldType.millis(),
             },
             new int[] {
               0, -1, -1, 1, 2, 3, 4, 5,
             });
     cYDTime = type;
   }
   return type;
 }
예제 #5
0
 /**
  * Gets a type that defines all standard fields.
  *
  * <ul>
  *   <li>years
  *   <li>months
  *   <li>weeks
  *   <li>days
  *   <li>hours
  *   <li>minutes
  *   <li>seconds
  *   <li>milliseconds
  * </ul>
  *
  * @return the period type
  */
 public static PeriodType standard() {
   PeriodType type = cStandard;
   if (type == null) {
     type =
         new PeriodType(
             "Standard",
             new DurationFieldType[] {
               DurationFieldType.years(), DurationFieldType.months(),
               DurationFieldType.weeks(), DurationFieldType.days(),
               DurationFieldType.hours(), DurationFieldType.minutes(),
               DurationFieldType.seconds(), DurationFieldType.millis(),
             },
             new int[] {
               0, 1, 2, 3, 4, 5, 6, 7,
             });
     cStandard = type;
   }
   return type;
 }
예제 #6
0
 /**
  * Gets a period type that contains the duration types of the array.
  *
  * <p>Only the 8 standard duration field types are supported.
  *
  * @param types the types to include in the array.
  * @return the period type
  * @since 1.1
  */
 public static synchronized PeriodType forFields(DurationFieldType[] types) {
   if (types == null || types.length == 0) {
     throw new IllegalArgumentException("Types array must not be null or empty");
   }
   for (int i = 0; i < types.length; i++) {
     if (types[i] == null) {
       throw new IllegalArgumentException("Types array must not contain null");
     }
   }
   Map<PeriodType, PeriodType> cache = cTypes;
   if (cTypes.isEmpty()) {
     cache.put(standard(), standard());
     cache.put(yearMonthDayTime(), yearMonthDayTime());
     cache.put(yearMonthDay(), yearMonthDay());
     cache.put(yearWeekDayTime(), yearWeekDayTime());
     cache.put(yearWeekDay(), yearWeekDay());
     cache.put(yearDayTime(), yearDayTime());
     cache.put(yearDay(), yearDay());
     cache.put(dayTime(), dayTime());
     cache.put(time(), time());
     cache.put(years(), years());
     cache.put(months(), months());
     cache.put(weeks(), weeks());
     cache.put(days(), days());
     cache.put(hours(), hours());
     cache.put(minutes(), minutes());
     cache.put(seconds(), seconds());
     cache.put(millis(), millis());
   }
   PeriodType inPartType = new PeriodType(null, types, null);
   Object cached = cache.get(inPartType);
   if (cached instanceof PeriodType) {
     return (PeriodType) cached;
   }
   if (cached != null) {
     throw new IllegalArgumentException("PeriodType does not support fields: " + cached);
   }
   PeriodType type = standard();
   List<DurationFieldType> list = new ArrayList<DurationFieldType>(Arrays.asList(types));
   if (list.remove(DurationFieldType.years()) == false) {
     type = type.withYearsRemoved();
   }
   if (list.remove(DurationFieldType.months()) == false) {
     type = type.withMonthsRemoved();
   }
   if (list.remove(DurationFieldType.weeks()) == false) {
     type = type.withWeeksRemoved();
   }
   if (list.remove(DurationFieldType.days()) == false) {
     type = type.withDaysRemoved();
   }
   if (list.remove(DurationFieldType.hours()) == false) {
     type = type.withHoursRemoved();
   }
   if (list.remove(DurationFieldType.minutes()) == false) {
     type = type.withMinutesRemoved();
   }
   if (list.remove(DurationFieldType.seconds()) == false) {
     type = type.withSecondsRemoved();
   }
   if (list.remove(DurationFieldType.millis()) == false) {
     type = type.withMillisRemoved();
   }
   if (list.size() > 0) {
     //            cache.put(inPartType, list);
     throw new IllegalArgumentException("PeriodType does not support fields: " + list);
   }
   // recheck cache in case initial array order was wrong
   PeriodType checkPartType = new PeriodType(null, type.iTypes, null);
   PeriodType checkedType = (PeriodType) cache.get(checkPartType);
   if (checkedType != null) {
     cache.put(inPartType, checkedType);
     return checkedType;
   }
   cache.put(inPartType, type);
   return type;
 }
예제 #7
0
 /**
  * Creates a <code>Years</code> representing the number of whole years between the two specified
  * datetimes. This method corectly handles any daylight savings time changes that may occur during
  * the interval.
  *
  * @param start the start instant, must not be null
  * @param end the end instant, must not be null
  * @return the period in years
  * @throws IllegalArgumentException if the instants are null or invalid
  */
 public static Years yearsBetween(ReadableInstant start, ReadableInstant end) {
   int amount = BaseSingleFieldPeriod.between(start, end, DurationFieldType.years());
   return Years.years(amount);
 }
예제 #8
0
 /**
  * Gets the duration field type, which is <code>years</code>.
  *
  * @return the period type
  */
 public DurationFieldType getFieldType() {
   return DurationFieldType.years();
 }