public int getMaximumValue(ReadablePartial partial) {
   if (partial.isSupported(DateTimeFieldType.monthOfYear())) {
     int month = partial.get(DateTimeFieldType.monthOfYear());
     if (partial.isSupported(DateTimeFieldType.year())) {
       int year = partial.get(DateTimeFieldType.year());
       return iChronology.getDaysInYearMonth(year, month);
     }
     return iChronology.getDaysInMonthMax(month);
   }
   return getMaximumValue();
 }
Beispiel #2
0
 public static long getTimeInterval(EnumQuotaPeriod period, DateTimeZone timezone) {
   DateTime ct = getCurrentWindow(period, timezone);
   DateTime tt = null;
   int y = ct.get(DateTimeFieldType.year());
   int m = ct.get(DateTimeFieldType.monthOfYear());
   int d = ct.get(DateTimeFieldType.dayOfMonth());
   int h = ct.get(DateTimeFieldType.hourOfDay());
   switch (period) {
     case Hourly:
       h++;
       tt = new DateTime(y, m, d, h, 0, timezone);
       break;
     case Daily:
       d++;
       tt = new DateTime(y, m, d, 0, 0, timezone);
       break;
     case Monthly:
       m++;
       tt = new DateTime(y, m, 0, 0, 0, timezone);
       break;
     case Yearly:
       y++;
       tt = new DateTime(y, m, 0, 0, 0, timezone);
       break;
   }
   Interval i = new Interval(ct, tt);
   return i.toDurationMillis();
 }
 // -----------------------------------------------------------------------
 public int[] add(ReadablePartial partial, int fieldIndex, int[] values, int valueToAdd) {
   // overridden as superclass algorithm can't handle
   // 2004-02-29 + 48 months -> 2008-02-29 type dates
   if (valueToAdd == 0) {
     return values;
   }
   if (partial.size() > 0
       && partial.getFieldType(0).equals(DateTimeFieldType.monthOfYear())
       && fieldIndex == 0) {
     // month is largest field and being added to, such as month-day
     int curMonth0 = partial.getValue(0) - 1;
     int newMonth = ((curMonth0 + (valueToAdd % 12) + 12) % 12) + 1;
     return set(partial, 0, values, newMonth);
   }
   if (DateTimeUtils.isContiguous(partial)) {
     long instant = 0L;
     for (int i = 0, isize = partial.size(); i < isize; i++) {
       instant = partial.getFieldType(i).getField(iChronology).set(instant, values[i]);
     }
     instant = add(instant, valueToAdd);
     return iChronology.get(partial, instant);
   } else {
     return super.add(partial, fieldIndex, values, valueToAdd);
   }
 }
Beispiel #4
0
 public static Partial getPartialFromString(String date) {
   Integer month = Integer.valueOf(date.substring(0, 2));
   Integer year = Integer.valueOf(date.substring(2));
   return new Partial(
       new DateTimeFieldType[] {DateTimeFieldType.year(), DateTimeFieldType.monthOfYear()},
       new int[] {year.intValue(), month.intValue()});
 }
Beispiel #5
0
  public static DateTime getCurrentWindow(
      EnumQuotaPeriod period, long timestamp, DateTimeZone timezone) {
    DateTime ct = new DateTime(timestamp);

    switch (period) {
      case Hourly:
        return new DateTime(
            ct.get(DateTimeFieldType.year()),
            ct.get(DateTimeFieldType.monthOfYear()),
            ct.get(DateTimeFieldType.dayOfMonth()),
            ct.get(DateTimeFieldType.hourOfDay()),
            0,
            timezone);
      case Daily:
        return new DateTime(
            ct.get(DateTimeFieldType.year()),
            ct.get(DateTimeFieldType.monthOfYear()),
            ct.get(DateTimeFieldType.dayOfMonth()),
            0,
            0,
            timezone);
      case Monthly:
        return new DateTime(
            ct.get(DateTimeFieldType.year()),
            ct.get(DateTimeFieldType.monthOfYear()),
            0,
            0,
            0,
            timezone);
      case Yearly:
        return new DateTime(ct.get(DateTimeFieldType.year()), 0, 0, 0, 0, timezone);

      default:
        break;
    }
    return null;
  }
 public int getMaximumValue(ReadablePartial partial, int[] values) {
   int size = partial.size();
   for (int i = 0; i < size; i++) {
     if (partial.getFieldType(i) == DateTimeFieldType.monthOfYear()) {
       int month = values[i];
       for (int j = 0; j < size; j++) {
         if (partial.getFieldType(j) == DateTimeFieldType.year()) {
           int year = values[j];
           return iChronology.getDaysInYearMonth(year, month);
         }
       }
       return iChronology.getDaysInMonthMax(month);
     }
   }
   return getMaximumValue();
 }
Beispiel #7
0
 /**
  * Get the month of year field for this chronology.
  *
  * @return DateTimeField or UnsupportedDateTimeField if unsupported
  */
 public DateTimeField monthOfYear() {
   return UnsupportedDateTimeField.getInstance(DateTimeFieldType.monthOfYear(), months());
 }
 /**
  * Restricted constructor.
  *
  * @param leapMonth the month of year that leaps
  */
 BasicMonthOfYearDateTimeField(BasicChronology chronology, int leapMonth) {
   super(DateTimeFieldType.monthOfYear(), chronology.getAverageMillisPerMonth());
   iChronology = chronology;
   iMax = iChronology.getMaxMonth();
   iLeapMonth = leapMonth;
 }
Beispiel #9
0
 private static YearMonthDay getDateFromPartial(Partial partial) {
   return new YearMonthDay(
       partial.get(DateTimeFieldType.year()), partial.get(DateTimeFieldType.monthOfYear()), 1);
 }
Beispiel #10
0
 public static Partial getPartialFromYearMonthDay(YearMonthDay day) {
   return new Partial(
       new DateTimeFieldType[] {DateTimeFieldType.year(), DateTimeFieldType.monthOfYear()},
       new int[] {day.getYear(), day.getMonthOfYear()});
 }
/**
 * Several discrete domains for dates.
 *
 * <p><strong>WARNING:</strong> When using these domains in a contiguous set, you
 * <strong>must</strong> ensure that your range has been {@link
 * PartitionDiscreteDomain#alignOut(Range) aligned} on this domain. Otherwise, you will experience
 * infinite loops.
 */
public class DateDiscreteDomain extends PartitionDiscreteDomain<Date> implements Serializable {

  private static final long serialVersionUID = 1L;

  /**
   * @return A discrete domain for the first day of each month at midnight.
   *     <p><strong>WARNING:</strong> When using this domain in a contiguous set, you
   *     <strong>must</strong> ensure that your range has been {@link
   *     DateDiscreteDomain#alignOut(Range) aligned} on this domain. Otherwise, you will experience
   *     infinite loops.
   */
  public static DateDiscreteDomain months() {
    return MONTHS;
  }

  private static final DateDiscreteDomain MONTHS =
      new DateDiscreteDomain(DurationFieldType.months(), DateTimeFieldType.monthOfYear()) {
        private static final long serialVersionUID = 1L;

        private Object readResolve() {
          return MONTHS;
        }

        @Override
        public String toString() {
          return "DateDiscreteDomain.months()";
        }
      };
  /**
   * @return A discrete domain for the first day of each week at midnight.
   *     <p><strong>WARNING:</strong> When using this domain in a contiguous set, you
   *     <strong>must</strong> ensure that your range has been {@link
   *     DateDiscreteDomain#align(Range) aligned} on this domain. Otherwise, you will experience
   *     infinite loops.
   */
  public static DateDiscreteDomain weeks() {
    return WEEKS;
  }

  private static final DateDiscreteDomain WEEKS =
      new DateDiscreteDomain(DurationFieldType.weeks(), DateTimeFieldType.weekOfWeekyear()) {
        private static final long serialVersionUID = 1L;

        private Object readResolve() {
          return WEEKS;
        }

        @Override
        public String toString() {
          return "DateDiscreteDomain.weeks()";
        }
      };

  private final DurationFieldType iterationFieldType;
  private final DateTimeFieldType roundFieldType;
  private final PeriodType periodType;

  private DateDiscreteDomain(
      DurationFieldType iterationFieldType, DateTimeFieldType roundFloorFieldType) {
    this.iterationFieldType = iterationFieldType;
    this.roundFieldType = roundFloorFieldType;
    this.periodType = PeriodType.forFields(new DurationFieldType[] {iterationFieldType});
  }

  @Override
  public Date next(Date value) {
    return internalAlignPrevious(value).withFieldAdded(iterationFieldType, 1).toDate();
  }

  @Override
  public Date previous(Date value) {
    return internalAlignNext(value).withFieldAdded(iterationFieldType, -1).toDate();
  }

  @Override
  public long distance(Date start, Date end) {
    return new Period(new LocalDate(start), new LocalDate(end), periodType).get(iterationFieldType);
  }

  @Override
  public Date alignNext(Date value) {
    return internalAlignNext(value).toDate();
  }

  @Override
  public Date alignPrevious(Date value) {
    return internalAlignPrevious(value).toDate();
  }

  private LocalDate internalAlignNext(Date value) {
    return new LocalDate(value).property(roundFieldType).roundCeilingCopy();
  }

  private LocalDate internalAlignPrevious(Date value) {
    return new LocalDate(value).property(roundFieldType).roundFloorCopy();
  }
}