Пример #1
0
  public Date calculateLeastMatchOnOrBefore(Date that) {
    if (equals(UNSPECIFIED)) // Performance improvement
    return MINIMUM_DATE;

    boolean matched = matches(that);
    GregorianCalendar gc = that.calculateGC();

    if (year != UNSPECIFIED_YEAR && year < that.year) // Performance improvement
    gc.add(Calendar.YEAR, year - that.year + 1);

    Date date = new Date(gc);
    while (true) {
      if (date.sameAs(MINIMUM_DATE)) return matched ? date : null;
      gc.add(Calendar.DATE, -1);
      date.resetTo(gc);
      boolean b = matches(date);
      if (b && !matched) matched = true;
      else if (matched && !b) break;
      if (year != UNSPECIFIED_YEAR && year > date.year) // Performance improvement
      return null;
    }

    gc.add(Calendar.DATE, 1);
    date.resetTo(gc);
    return date;
  }
Пример #2
0
  public Date calculateGreatestMatchOnOrBefore(Date that) {
    if (equals(UNSPECIFIED)) // Performance improvement
    return null;

    GregorianCalendar gc = that.calculateGC();
    if (year != UNSPECIFIED_YEAR && year < that.year) // Performance improvement
    gc.add(Calendar.YEAR, year - that.year + 1);

    if (!that.sameAs(MAXIMUM_DATE)) {
      // Start a day ahead
      gc.add(Calendar.DATE, 1);
    }
    Date date = new Date(gc);

    boolean matched = matches(date);
    while (true) {
      gc.add(Calendar.DATE, -1);
      date.resetTo(gc);
      boolean b = matches(date);
      if (!b && matched) matched = false;
      else if (!matched && b) break;
      if (date.sameAs(MINIMUM_DATE)) return null;
      if (year != UNSPECIFIED_YEAR && year > date.year) // Performance improvement
      return null;
    }

    return date;
  }
Пример #3
0
  public Date calculateGreatestMatchOnOrAfter(Date that) {
    if (equals(UNSPECIFIED)) return MAXIMUM_DATE;

    boolean matched = matches(that);
    GregorianCalendar gc = that.calculateGC();

    Date date = new Date(gc);
    while (true) {
      gc.add(Calendar.DATE, 1);
      date.resetTo(gc);
      boolean b = matches(date);
      if (b && !matched) matched = true;
      else if (matched && !b) break;
      if (date.sameAs(MAXIMUM_DATE)) return date;
    }

    gc.add(Calendar.DATE, -1);
    date.resetTo(gc);
    return date;
  }
Пример #4
0
  public Date calculateLeastMatchOnOrAfter(Date that) {
    GregorianCalendar gc = that.calculateGC();
    if (!that.sameAs(MINIMUM_DATE)) {
      // Start a day behind
      gc.add(Calendar.DATE, -1);
    }
    Date date = new Date(gc);

    boolean matched = matches(date);
    while (true) {
      gc.add(Calendar.DATE, 1);
      date.resetTo(gc);
      boolean b = matches(date);
      if (!b && matched) matched = false;
      else if (!matched && b) break;
      if (date.sameAs(MAXIMUM_DATE)) return null;
    }

    return date;
  }
Пример #5
0
 public Date(GregorianCalendar gc) {
   resetTo(gc);
 }