Package sun.util.calendar

Examples of sun.util.calendar.CalendarDate


    // The day might have changed, which could happen if
    // the daylight saving time transition brings it to
    // the next day, although it's very unlikely. But we
    // have to make sure not to change the larger fields.
    CalendarDate d = calsys.getCalendarDate(time, getZone());
    if (internalGet(DAY_OF_MONTH) != d.getDayOfMonth()) {
        d.setDate(internalGet(YEAR),
            internalGet(MONTH) + 1,
            internalGet(DAY_OF_MONTH));
        if (field == HOUR) {
      assert (internalGet(AM_PM) == PM);
      d.addHours(+12); // restore PM
        }
        time = calsys.getTime(d);
    }
    int hourOfDay = d.getHours();
    internalSet(field, hourOfDay % unit);
    if (field == HOUR) {
        internalSet(HOUR_OF_DAY, hourOfDay);
    } else {
        internalSet(AM_PM, hourOfDay / 12);
        internalSet(HOUR, hourOfDay % 12);
    }

    // Time zone offset and/or daylight saving might have changed.
    int zoneOffset = d.getZoneOffset();
    int saving = d.getDaylightSaving();
    internalSet(ZONE_OFFSET, zoneOffset - saving);
    internalSet(DST_OFFSET, saving);
    return;
      }

        case MONTH:
            // Rolling the month involves both pinning the final value to [0, 11]
            // and adjusting the DAY_OF_MONTH if necessary.  We only adjust the
            // DAY_OF_MONTH if, after updating the MONTH field, it is illegal.
            // E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.
            {
    if (!isCutoverYear(cdate.getNormalizedYear())) {
        int mon = (internalGet(MONTH) + amount) % 12;
        if (mon < 0) {
      mon += 12;
        }
        set(MONTH, mon);
               
        // Keep the day of month in the range.  We don't want to spill over
        // into the next month; e.g., we don't want jan31 + 1 mo -> feb31 ->
        // mar3.
        int monthLen = monthLength(mon);
        if (internalGet(DAY_OF_MONTH) > monthLen) {
      set(DAY_OF_MONTH, monthLen);
        }
    } else {
        // We need to take care of different lengths in
        // year and month due to the cutover.
        int yearLength = getActualMaximum(MONTH) + 1;
        int mon = (internalGet(MONTH) + amount) % yearLength;
        if (mon < 0) {
      mon += yearLength;
        }
        set(MONTH, mon);
        int monthLen = getActualMaximum(DAY_OF_MONTH);
        if (internalGet(DAY_OF_MONTH) > monthLen) {
      set(DAY_OF_MONTH, monthLen);
        }
    }
    return;
            }

        case WEEK_OF_YEAR:
      {
    int y = cdate.getNormalizedYear();
    max = getActualMaximum(WEEK_OF_YEAR);
    set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
    int woy = internalGet(WEEK_OF_YEAR);
    int value = woy + amount;
    if (!isCutoverYear(y)) {
        // If the new value is in between min and max
        // (exclusive), then we can use the value.
        if (value > min && value < max) {
      set(WEEK_OF_YEAR, value);
      return;
        }
        long fd = getCurrentFixedDate();
        // Make sure that the min week has the current DAY_OF_WEEK
        long day1 = fd - (7 * (woy - min));
        if (calsys.getYearFromFixedDate(day1) != y) {
      min++;
        }

        // Make sure the same thing for the max week
        fd += 7 * (max - internalGet(WEEK_OF_YEAR));
        if (calsys.getYearFromFixedDate(fd) != y) {
      max--;
        }
        break;
    }

    // Handle cutover here.
    long fd = getCurrentFixedDate();
    BaseCalendar cal;
    if (gregorianCutoverYear == gregorianCutoverYearJulian) {
        cal = getCutoverCalendarSystem();
    } else if (y == gregorianCutoverYear) {
        cal = gcal;
    } else {
        cal = getJulianCalendarSystem();
    }
    long day1 = fd - (7 * (woy - min));
    // Make sure that the min week has the current DAY_OF_WEEK
    if (cal.getYearFromFixedDate(day1) != y) {
        min++;
    }

    // Make sure the same thing for the max week
    fd += 7 * (max - woy);
    cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
    if (cal.getYearFromFixedDate(fd) != y) {
        max--;
    }
    // value: the new WEEK_OF_YEAR which must be converted
    // to month and day of month.
    value = getRolledValue(woy, amount, min, max) - 1;
    BaseCalendar.Date d = getCalendarDate(day1 + value * 7);
    set(MONTH, d.getMonth() - 1);
    set(DAY_OF_MONTH, d.getDayOfMonth());
    return;
      }

        case WEEK_OF_MONTH:
      {
    boolean isCutoverYear = isCutoverYear(cdate.getNormalizedYear());
    // dow: relative day of week from first day of week
    int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
    if (dow < 0) {
        dow += 7;
    }

    long fd = getCurrentFixedDate();
    long month1;   // fixed date of the first day (usually 1) of the month
    int monthLength; // actual month length
    if (isCutoverYear) {
        month1 = getFixedDateMonth1(cdate, fd);
        monthLength = actualMonthLength();
    } else {
        month1 = fd - internalGet(DAY_OF_MONTH) + 1;
        monthLength = calsys.getMonthLength(cdate);
    }

    // the first day of week of the month.
    long monthDay1st = calsys.getDayOfWeekDateOnOrBefore(month1 + 6,
                     getFirstDayOfWeek());
    // if the week has enough days to form a week, the
    // week starts from the previous month.
    if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) {
        monthDay1st -= 7;
    }
    max = getActualMaximum(field);

    // value: the new WEEK_OF_MONTH value
    int value = getRolledValue(internalGet(field), amount, 1, max) - 1;

    // nfd: fixed date of the rolled date
    long nfd = monthDay1st + value * 7 + dow;

    // Unlike WEEK_OF_YEAR, we need to change day of week if the
    // nfd is out of the month.
    if (nfd < month1) {
        nfd = month1;
    } else if (nfd >= (month1 + monthLength)) {
        nfd = month1 + monthLength - 1;
    }
    int dayOfMonth;
    if (isCutoverYear) {
        // If we are in the cutover year, convert nfd to
        // its calendar date and use dayOfMonth.
        BaseCalendar.Date d = getCalendarDate(nfd);
        dayOfMonth = d.getDayOfMonth();
    } else {
        dayOfMonth = (int)(nfd - month1) + 1;
    }
    set(DAY_OF_MONTH, dayOfMonth);
    return;
      }

        case DAY_OF_MONTH:
      {
    if (!isCutoverYear(cdate.getNormalizedYear())) {
        max = calsys.getMonthLength(cdate);
        break;
    }

    // Cutover year handling
    long fd = getCurrentFixedDate();
    long month1 = getFixedDateMonth1(cdate, fd);
    // It may not be a regular month. Convert the date and range to
    // the relative values, perform the roll, and
    // convert the result back to the rolled date.
    int value = getRolledValue((int)(fd - month1), amount, 0, actualMonthLength() - 1);
    BaseCalendar.Date d = getCalendarDate(month1 + value);
    assert d.getMonth()-1 == internalGet(MONTH);
    set(DAY_OF_MONTH, d.getDayOfMonth());
    return;
      }

        case DAY_OF_YEAR:
      {
    max = getActualMaximum(field);
    if (!isCutoverYear(cdate.getNormalizedYear())) {
        break;
    }

    // Handle cutover here.
    long fd = getCurrentFixedDate();
    long jan1 = fd - internalGet(DAY_OF_YEAR) + 1;
    int value = getRolledValue((int)(fd - jan1) + 1, amount, min, max);
    BaseCalendar.Date d = getCalendarDate(jan1 + value - 1);
    set(MONTH, d.getMonth() - 1);
    set(DAY_OF_MONTH, d.getDayOfMonth());
    return;
      }

        case DAY_OF_WEEK:
      {
    if (!isCutoverYear(cdate.getNormalizedYear())) {
        // If the week of year is in the same year, we can
        // just change DAY_OF_WEEK.
        int weekOfYear = internalGet(WEEK_OF_YEAR);
        if (weekOfYear > 1 && weekOfYear < 52) {
      set(WEEK_OF_YEAR, weekOfYear); // update stamp[WEEK_OF_YEAR]
      max = SATURDAY;
      break;
        }
    }

    // We need to handle it in a different way around year
    // boundaries and in the cutover year. Note that
    // changing era and year values violates the roll
    // rule: not changing larger calendar fields...
    amount %= 7;
    if (amount == 0) {
        return;
    }
    long fd = getCurrentFixedDate();
    long dowFirst = calsys.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek());
    fd += amount;
    if (fd < dowFirst) {
        fd += 7;
    } else if (fd >= dowFirst + 7) {
        fd -= 7;
    }
    BaseCalendar.Date d = getCalendarDate(fd);
    set(ERA, (d.getNormalizedYear() <= 0 ? BCE : CE));
    set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth());
    return;
      }

        case DAY_OF_WEEK_IN_MONTH:
            {
    min = 1; // after normalized, min should be 1.
    if (!isCutoverYear(cdate.getNormalizedYear())) {
        int dom = internalGet(DAY_OF_MONTH);
        int monthLength = calsys.getMonthLength(cdate);
        int lastDays = monthLength % 7;
        max = monthLength / 7;
        int x = (dom - 1) % 7;
        if (x < lastDays) {
      max++;
        }
        set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
        break;
    }

    // Cutover year handling
    long fd = getCurrentFixedDate();
    long month1 = getFixedDateMonth1(cdate, fd);
    int monthLength = actualMonthLength();
    int lastDays = monthLength % 7;
    max = monthLength / 7;
    int x = (int)(fd - month1) % 7;
    if (x < lastDays) {
        max++;
    }
    int value = getRolledValue(internalGet(field), amount, min, max) - 1;
    fd = month1 + value * 7 + x;
    BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
    BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
    cal.getCalendarDateFromFixedDate(d, fd);
    set(DAY_OF_MONTH, d.getDayOfMonth());
    return;
            }
  }

  set(field, getRolledValue(internalGet(field), amount, min, max));
View Full Code Here


        case WEEK_OF_YEAR:
      {
    if (!gc.isCutoverYear(normalizedYear)) {
        // Get the day of week of January 1 of the year
        CalendarDate d = cal.newCalendarDate(TimeZone.NO_TIMEZONE);
        d.setDate(date.getYear(), BaseCalendar.JANUARY, 1);
        int dayOfWeek = cal.getDayOfWeek(d);
        // Normalize the day of week with the firstDayOfWeek value
        dayOfWeek -= getFirstDayOfWeek();
        if (dayOfWeek < 0) {
      dayOfWeek += 7;
        }
        value = 52;
        int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;
        if ((magic == 6) ||
      (date.isLeapYear() && (magic == 5 || magic == 12))) {
      value++;
        }
        break;
    }

    if (gc == this) {
        gc = (GregorianCalendar) gc.clone();
    }
    gc.set(DAY_OF_YEAR, getActualMaximum(DAY_OF_YEAR));
    value = gc.get(WEEK_OF_YEAR);
      }
      break;

        case WEEK_OF_MONTH:
      {
    if (!gc.isCutoverYear(normalizedYear)) {
        CalendarDate d = cal.newCalendarDate(null);
        d.setDate(date.getYear(), date.getMonth(), 1);
        int dayOfWeek = cal.getDayOfWeek(d);
        int monthLength = cal.getMonthLength(d);
        dayOfWeek -= getFirstDayOfWeek();
        if (dayOfWeek < 0) {
      dayOfWeek += 7;
        }
        int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week
        value = 3;
        if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {
      value++;
        }
        monthLength -= nDaysFirstWeek + 7 * 3;
        if (monthLength > 0) {
      value++;
      if (monthLength > 7) {
          value++;
      }
        }
        break;
    }

    // Cutover year handling
    if (gc == this) {
        gc = (GregorianCalendar) gc.clone();
    }
    int y = gc.internalGet(YEAR);
    int m = gc.internalGet(MONTH);
    do {
        value = gc.get(WEEK_OF_MONTH);
        gc.add(WEEK_OF_MONTH, +1);
    } while (gc.get(YEAR) == y && gc.get(MONTH) == m);
      }
      break;

        case DAY_OF_WEEK_IN_MONTH:
      {
    // may be in the Gregorian cutover month
    int ndays, dow1;
    int dow = date.getDayOfWeek();
    if (!gc.isCutoverYear(normalizedYear)) {
        BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
        ndays = cal.getMonthLength(d);
        d.setDayOfMonth(1);
        cal.normalize(d);
        dow1 = d.getDayOfWeek();
    } else {
        // Let a cloned GregorianCalendar take care of the cutover cases.
        if (gc == this) {
      gc = (GregorianCalendar) clone();
        }
        ndays = gc.actualMonthLength();
        gc.set(DAY_OF_MONTH, gc.getActualMinimum(DAY_OF_MONTH));
        dow1 = gc.get(DAY_OF_WEEK);
    }
    int x = dow - dow1;
    if (x < 0) {
        x += 7;
    }
    ndays -= x;
    value = (ndays + 6) / 7;
      }
      break;

  case YEAR:
            /* The year computation is no different, in principle, from the
             * others, however, the range of possible maxima is large.  In
             * addition, the way we know we've exceeded the range is different.
             * For these reasons, we use the special case code below to handle
             * this field.
             *
             * The actual maxima for YEAR depend on the type of calendar:
             *
             *     Gregorian = May 17, 292275056 BCE - Aug 17, 292278994 CE
             *     Julian    = Dec  2, 292269055 BCE - Jan  3, 292272993 CE
             *     Hybrid    = Dec  2, 292269055 BCE - Aug 17, 292278994 CE
             *
             * We know we've exceeded the maximum when either the month, date,
             * time, or era changes in response to setting the year.  We don't
             * check for month, date, and time here because the year and era are
             * sufficient to detect an invalid year setting.  NOTE: If code is
             * added to check the month and date in the future for some reason,
             * Feb 29 must be allowed to shift to Mar 1 when setting the year.
             */
      {
    if (gc == this) {
        gc = (GregorianCalendar) clone();
    }

    // Calculate the millisecond offset from the beginning
    // of the year of this calendar and adjust the max
    // year value if we are beyond the limit in the max
    // year.
    long current = gc.getYearOffsetInMillis();

    if (gc.internalGetEra() == CE) {
        gc.setTimeInMillis(Long.MAX_VALUE);
        value = gc.get(YEAR);
        long maxEnd = gc.getYearOffsetInMillis();
        if (current > maxEnd) {
      value--;
        }
    } else {
        CalendarSystem mincal = gc.getTimeInMillis() >= gregorianCutover ?
      gcal : getJulianCalendarSystem();
        CalendarDate d = mincal.getCalendarDate(Long.MIN_VALUE, getZone());
        long maxEnd = (cal.getDayOfYear(d) - 1) * 24 + d.getHours();
        maxEnd *= 60;
        maxEnd += d.getMinutes();
        maxEnd *= 60;
        maxEnd += d.getSeconds();
        maxEnd *= 1000;
        maxEnd += d.getMillis();
        value = d.getYear();
        if (value <= 0) {
      assert mincal == gcal;
      value = 1 - value;
        }
        if (current < maxEnd) {
View Full Code Here

     * Returns the calendar system for dates before the cutover date
     * in the cutover year. If the cutover date is January 1, the
     * method returns Gregorian. Otherwise, Julian.
     */
    private BaseCalendar getCutoverCalendarSystem() {
  CalendarDate date = getGregorianCutoverDate();
  if (date.getMonth() == BaseCalendar.JANUARY
      && date.getDayOfMonth() == 1) {
      return gcal;
  }
  return getJulianCalendarSystem();
    }
View Full Code Here

    // The day might have changed, which could happen if
    // the daylight saving time transition brings it to
    // the next day, although it's very unlikely. But we
    // have to make sure not to change the larger fields.
    CalendarDate d = jcal.getCalendarDate(time, getZone());
    if (internalGet(DAY_OF_MONTH) != d.getDayOfMonth()) {
        d.setEra(jdate.getEra());
        d.setDate(internalGet(YEAR),
            internalGet(MONTH) + 1,
            internalGet(DAY_OF_MONTH));
        if (field == HOUR) {
      assert (internalGet(AM_PM) == PM);
      d.addHours(+12); // restore PM
        }
        time = jcal.getTime(d);
    }
    int hourOfDay = d.getHours();
    internalSet(field, hourOfDay % unit);
    if (field == HOUR) {
        internalSet(HOUR_OF_DAY, hourOfDay);
    } else {
        internalSet(AM_PM, hourOfDay / 12);
        internalSet(HOUR, hourOfDay % 12);
    }

    // Time zone offset and/or daylight saving might have changed.
    int zoneOffset = d.getZoneOffset();
    int saving = d.getDaylightSaving();
    internalSet(ZONE_OFFSET, zoneOffset - saving);
    internalSet(DST_OFFSET, saving);
    return;
      }

        case YEAR:
      min = getActualMinimum(field);
      max = getActualMaximum(field);
      break;

        case MONTH:
            // Rolling the month involves both pinning the final value to [0, 11]
            // and adjusting the DAY_OF_MONTH if necessary.  We only adjust the
            // DAY_OF_MONTH if, after updating the MONTH field, it is illegal.
            // E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.
            {
    if (!isTransitionYear(jdate.getNormalizedYear())) {
        int year = jdate.getYear();
        if (year == getMaximum(YEAR)) {
      CalendarDate jd = jcal.getCalendarDate(time, getZone());
      CalendarDate d = jcal.getCalendarDate(Long.MAX_VALUE, getZone());
      max = d.getMonth() - 1;
      int n = getRolledValue(internalGet(field), amount, min, max);
      if (n == max) {
          // To avoid overflow, use an equivalent year.
          jd.addYear(-400);
          jd.setMonth(n + 1);
          if (jd.getDayOfMonth() > d.getDayOfMonth()) {
        jd.setDayOfMonth(d.getDayOfMonth());
        jcal.normalize(jd);
          }
          if (jd.getDayOfMonth() == d.getDayOfMonth()
        && jd.getTimeOfDay() > d.getTimeOfDay()) {
        jd.setMonth(n + 1);
        jd.setDayOfMonth(d.getDayOfMonth() - 1);
        jcal.normalize(jd);
        // Month may have changed by the normalization.
        n = jd.getMonth() - 1;
          }
          set(DAY_OF_MONTH, jd.getDayOfMonth());
      }
      set(MONTH, n);
        } else if (year == getMinimum(YEAR)) {
      CalendarDate jd = jcal.getCalendarDate(time, getZone());
      CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
      min = d.getMonth() - 1;
      int n = getRolledValue(internalGet(field), amount, min, max);
      if (n == min) {
          // To avoid underflow, use an equivalent year.
          jd.addYear(+400);
          jd.setMonth(n + 1);
          if (jd.getDayOfMonth() < d.getDayOfMonth()) {
        jd.setDayOfMonth(d.getDayOfMonth());
        jcal.normalize(jd);
          }
          if (jd.getDayOfMonth() == d.getDayOfMonth()
        && jd.getTimeOfDay() < d.getTimeOfDay()) {
        jd.setMonth(n + 1);
        jd.setDayOfMonth(d.getDayOfMonth() + 1);
        jcal.normalize(jd);
        // Month may have changed by the normalization.
        n = jd.getMonth() - 1;
          }
          set(DAY_OF_MONTH, jd.getDayOfMonth());
      }
      set(MONTH, n);
        } else {
      int mon = (internalGet(MONTH) + amount) % 12;
      if (mon < 0) {
          mon += 12;
      }
      set(MONTH, mon);
               
      // Keep the day of month in the range.  We
      // don't want to spill over into the next
      // month; e.g., we don't want jan31 + 1 mo ->
      // feb31 -> mar3.
      int monthLen = monthLength(mon);
      if (internalGet(DAY_OF_MONTH) > monthLen) {
          set(DAY_OF_MONTH, monthLen);
      }
        }
    } else {
        int eraIndex = getEraIndex(jdate);
        CalendarDate transition = null;
        if (jdate.getYear() == 1) {
      transition = eras[eraIndex].getSinceDate();
      min = transition.getMonth() - 1;
        } else {
      if (eraIndex < eras.length - 1) {
          transition = eras[eraIndex + 1].getSinceDate();
          if (transition.getYear() == jdate.getNormalizedYear()) {
        max = transition.getMonth() - 1;
        if (transition.getDayOfMonth() == 1) {
            max--;
        }
          }
      }
        }

        if (min == max) {
      // The year has only one month. No need to
      // process further. (Showa Gan-nen (year 1)
      // and the last year have only one month.)
      return;
        }
        int n = getRolledValue(internalGet(field), amount, min, max);
        set(MONTH, n);
        if (n == min) {
      if (!(transition.getMonth() == BaseCalendar.JANUARY
            && transition.getDayOfMonth() == 1)) {
          if (jdate.getDayOfMonth() < transition.getDayOfMonth()) {
        set(DAY_OF_MONTH, transition.getDayOfMonth());
          }
      }
        } else if (n == max && (transition.getMonth() - 1 == n)) {
      int dom = transition.getDayOfMonth();
      if (jdate.getDayOfMonth() >= dom) {
          set(DAY_OF_MONTH, dom - 1);
      }
        }
    }
    return;
            }

        case WEEK_OF_YEAR:
      {
    int y = jdate.getNormalizedYear();
    max = getActualMaximum(WEEK_OF_YEAR);
    set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK)); // update stamp[field]
    int woy = internalGet(WEEK_OF_YEAR);
    int value = woy + amount;
    if (!isTransitionYear(jdate.getNormalizedYear())) {
        int year = jdate.getYear();
        if (year == getMaximum(YEAR)) {
      max = getActualMaximum(WEEK_OF_YEAR);
        } else if (year == getMinimum(YEAR)) {
      min = getActualMinimum(WEEK_OF_YEAR);
      max = getActualMaximum(WEEK_OF_YEAR);
      if (value > min && value < max) {
          set(WEEK_OF_YEAR, value);
          return;
      }
     
        }
        // If the new value is in between min and max
        // (exclusive), then we can use the value.
        if (value > min && value < max) {
      set(WEEK_OF_YEAR, value);
      return;
        }
        long fd = cachedFixedDate;
        // Make sure that the min week has the current DAY_OF_WEEK
        long day1 = fd - (7 * (woy - min));
        if (year != getMinimum(YEAR)) {
      if (gcal.getYearFromFixedDate(day1) != y) {
          min++;
      }
        } else {
      CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
      if (day1 < jcal.getFixedDate(d)) {
          min++;
      }
        }

        // Make sure the same thing for the max week
        fd += 7 * (max - internalGet(WEEK_OF_YEAR));
        if (gcal.getYearFromFixedDate(fd) != y) {
      max--;
        }
        break;
    }

    // Handle transition here.
    long fd = cachedFixedDate;
    long day1 = fd - (7 * (woy - min));
    // Make sure that the min week has the current DAY_OF_WEEK
    LocalGregorianCalendar.Date d = getCalendarDate(day1);
    if (!(d.getEra() == jdate.getEra() && d.getYear() == jdate.getYear())) {
        min++;
    }

    // Make sure the same thing for the max week
    fd += 7 * (max - woy);
    jcal.getCalendarDateFromFixedDate(d, fd);
    if (!(d.getEra() == jdate.getEra() && d.getYear() == jdate.getYear())) {
        max--;
    }
    // value: the new WEEK_OF_YEAR which must be converted
    // to month and day of month.
    value = getRolledValue(woy, amount, min, max) - 1;
    d = getCalendarDate(day1 + value * 7);
    set(MONTH, d.getMonth() - 1);
    set(DAY_OF_MONTH, d.getDayOfMonth());
    return;
      }

        case WEEK_OF_MONTH:
      {
    boolean isTransitionYear = isTransitionYear(jdate.getNormalizedYear());
    // dow: relative day of week from the first day of week
    int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
    if (dow < 0) {
        dow += 7;
    }

    long fd = cachedFixedDate;
    long month1;   // fixed date of the first day (usually 1) of the month
    int monthLength; // actual month length
    if (isTransitionYear) {
        month1 = getFixedDateMonth1(jdate, fd);
        monthLength = actualMonthLength();
    } else {
        month1 = fd - internalGet(DAY_OF_MONTH) + 1;
        monthLength = jcal.getMonthLength(jdate);
    }

    // the first day of week of the month.
    long monthDay1st = jcal.getDayOfWeekDateOnOrBefore(month1 + 6,
                   getFirstDayOfWeek());
    // if the week has enough days to form a week, the
    // week starts from the previous month.
    if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) {
        monthDay1st -= 7;
    }
    max = getActualMaximum(field);

    // value: the new WEEK_OF_MONTH value
    int value = getRolledValue(internalGet(field), amount, 1, max) - 1;

    // nfd: fixed date of the rolled date
    long nfd = monthDay1st + value * 7 + dow;

    // Unlike WEEK_OF_YEAR, we need to change day of week if the
    // nfd is out of the month.
    if (nfd < month1) {
        nfd = month1;
    } else if (nfd >= (month1 + monthLength)) {
        nfd = month1 + monthLength - 1;
    }
    set(DAY_OF_MONTH, (int)(nfd - month1) + 1);
    return;
      }

        case DAY_OF_MONTH:
      {
    if (!isTransitionYear(jdate.getNormalizedYear())) {
        max = jcal.getMonthLength(jdate);
        break;
    }

    // TODO: Need to change the spec to be usable DAY_OF_MONTH rolling...

    // Transition handling. We can't change year and era
    // values here due to the Calendar roll spec!
    long month1 = getFixedDateMonth1(jdate, cachedFixedDate);

    // It may not be a regular month. Convert the date and range to
    // the relative values, perform the roll, and
    // convert the result back to the rolled date.
    int value = getRolledValue((int)(cachedFixedDate - month1), amount,
             0, actualMonthLength() - 1);
    LocalGregorianCalendar.Date d = getCalendarDate(month1 + value);
    assert getEraIndex(d) == internalGetEra()
        && d.getYear() == internalGet(YEAR) && d.getMonth()-1 == internalGet(MONTH);
    set(DAY_OF_MONTH, d.getDayOfMonth());
    return;
      }

        case DAY_OF_YEAR:
      {
    max = getActualMaximum(field);
    if (!isTransitionYear(jdate.getNormalizedYear())) {
        break;
    }

    // Handle transition. We can't change year and era values
    // here due to the Calendar roll spec.
    int value = getRolledValue(internalGet(DAY_OF_YEAR), amount, min, max);
    long jan0 = cachedFixedDate - internalGet(DAY_OF_YEAR);
    LocalGregorianCalendar.Date d = getCalendarDate(jan0 + value);
    assert getEraIndex(d) == internalGetEra() && d.getYear() == internalGet(YEAR);
    set(MONTH, d.getMonth() - 1);
    set(DAY_OF_MONTH, d.getDayOfMonth());
    return;
      }

        case DAY_OF_WEEK:
      {
    int normalizedYear = jdate.getNormalizedYear();
    if (!isTransitionYear(normalizedYear) && !isTransitionYear(normalizedYear - 1)) {
        // If the week of year is in the same year, we can
        // just change DAY_OF_WEEK.
        int weekOfYear = internalGet(WEEK_OF_YEAR);
        if (weekOfYear > 1 && weekOfYear < 52) {
      set(WEEK_OF_YEAR, internalGet(WEEK_OF_YEAR));
      max = SATURDAY;
      break;
        }
    }

    // We need to handle it in a different way around year
    // boundaries and in the transition year. Note that
    // changing era and year values violates the roll
    // rule: not changing larger calendar fields...
    amount %= 7;
    if (amount == 0) {
        return;
    }
    long fd = cachedFixedDate;
    long dowFirst = jcal.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek());
    fd += amount;
    if (fd < dowFirst) {
        fd += 7;
    } else if (fd >= dowFirst + 7) {
        fd -= 7;
    }
    LocalGregorianCalendar.Date d = getCalendarDate(fd);
    set(ERA, getEraIndex(d));
    set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth());
    return;
      }

        case DAY_OF_WEEK_IN_MONTH:
            {
    min = 1; // after having normalized, min should be 1.
    if (!isTransitionYear(jdate.getNormalizedYear())) {
        int dom = internalGet(DAY_OF_MONTH);
        int monthLength = jcal.getMonthLength(jdate);
        int lastDays = monthLength % 7;
        max = monthLength / 7;
        int x = (dom - 1) % 7;
        if (x < lastDays) {
      max++;
        }
        set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
        break;
    }

    // Transition year handling.
    long fd = cachedFixedDate;
    long month1 = getFixedDateMonth1(jdate, fd);
    int monthLength = actualMonthLength();
    int lastDays = monthLength % 7;
    max = monthLength / 7;
    int x = (int)(fd - month1) % 7;
    if (x < lastDays) {
        max++;
    }
    int value = getRolledValue(internalGet(field), amount, min, max) - 1;
    fd = month1 + value * 7 + x;
    LocalGregorianCalendar.Date d = getCalendarDate(fd);
    set(DAY_OF_MONTH, d.getDayOfMonth());
    return;
            }
  }

  set(field, getRolledValue(internalGet(field), amount, min, max));
View Full Code Here

  case YEAR:
      {
    if (eraIndex > BEFORE_MEIJI) {
        value = 1;
        long since = eras[eraIndex].getSince(getZone());
        CalendarDate d = jcal.getCalendarDate(since, getZone());
        // Use the same year in jd to take care of leap
        // years. i.e., both jd and d must agree on leap
        // or common years.
        jd.setYear(d.getYear());
        jcal.normalize(jd);
        assert jd.isLeapYear() == d.isLeapYear();
        if (getYearOffsetInMillis(jd) < getYearOffsetInMillis(d)) {
      value++;
        }
    } else {
        value = getMinimum(field);
        CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
        // Use an equvalent year of d.getYear() if
        // possible. Otherwise, ignore the leap year and
        // common year difference.
        int y = d.getYear();
        if (y > 400) {
      y -= 400;
        }
        jd.setYear(y);
        jcal.normalize(jd);
        if (getYearOffsetInMillis(jd) < getYearOffsetInMillis(d)) {
      value++;
        }
    }
      }
      break;

  case MONTH:
      {
    // In Before Meiji and Meiji, January is the first month.
    if (eraIndex > MEIJI && jd.getYear() == 1) {
        long since = eras[eraIndex].getSince(getZone());
        CalendarDate d = jcal.getCalendarDate(since, getZone());
        value = d.getMonth() - 1;
        if (jd.getDayOfMonth() < d.getDayOfMonth()) {
      value++;
        }
    }
      }
      break;

  case WEEK_OF_YEAR:
      {
    value = 1;
    CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
    // shift 400 years to avoid underflow
    d.addYear(+400);
    jcal.normalize(d);
    jd.setEra(d.getEra());
    jd.setYear(d.getYear());
    jcal.normalize(jd);

    long jan1 = jcal.getFixedDate(d);
    long fd = jcal.getFixedDate(jd);
    int woy = getWeekNumber(jan1, fd);
    long day1 = fd - (7 * (woy - 1));
    if ((day1 < jan1) ||
        (day1 == jan1 &&
         jd.getTimeOfDay() < d.getTimeOfDay())) {
        value++;
    }
      }
      break;
  }
View Full Code Here

      eraIndex++;
      assert eraIndex < eras.length;
        }
        long transition = sinceFixedDates[eraIndex];
        long fd = jc.cachedFixedDate;
        CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
        d.setDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1);
        if (fd < transition) {
      value = (int)(transition - gcal.getFixedDate(d));
        } else {
      d.addYear(+1);
      value = (int)(gcal.getFixedDate(d) - transition);
        }
    } else {
        LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE,
                   getZone());
        if (date.getEra() == d.getEra() && date.getYear() == d.getYear()) {
      long fd = jcal.getFixedDate(d);
      long jan1 = getFixedDateJan1(d, fd);
      value = (int)(fd - jan1) + 1;
        } else if (date.getYear() == getMinimum(YEAR)) {
      CalendarDate d1 = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
      long fd1 = jcal.getFixedDate(d1);
      d1.addYear(1);
      d1.setMonth(BaseCalendar.JANUARY).setDayOfMonth(1);
      jcal.normalize(d1);
      long fd2 = jcal.getFixedDate(d1);
      value = (int)(fd2 - fd1);
        } else {
      value = jcal.getYearLength(date);
        }
    }
      }
      break;

        case WEEK_OF_YEAR:
      {
    if (!isTransitionYear(date.getNormalizedYear())) {
        LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE,
                    getZone());
        if (date.getEra() == jd.getEra() && date.getYear() == jd.getYear()) {
      long fd = jcal.getFixedDate(jd);
      long jan1 = getFixedDateJan1(jd, fd);
      value = getWeekNumber(jan1, fd);
        } else if (date.getEra() == null && date.getYear() == getMinimum(YEAR)) {
      CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
      // shift 400 years to avoid underflow
      d.addYear(+400);
      jcal.normalize(d);
      jd.setEra(d.getEra());
      jd.setDate(d.getYear() + 1, BaseCalendar.JANUARY, 1);
      jcal.normalize(jd);
      long jan1 = jcal.getFixedDate(d);
      long nextJan1 = jcal.getFixedDate(jd);
      long nextJan1st = jcal.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
                    getFirstDayOfWeek());
      int ndays = (int)(nextJan1st - nextJan1);
      if (ndays >= getMinimalDaysInFirstWeek()) {
          nextJan1st -= 7;
      }
      value = getWeekNumber(jan1, nextJan1st);
        } else {
      // Get the day of week of January 1 of the year
      CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
      d.setDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1);
      int dayOfWeek = gcal.getDayOfWeek(d);
      // Normalize the day of week with the firstDayOfWeek value
      dayOfWeek -= getFirstDayOfWeek();
      if (dayOfWeek < 0) {
          dayOfWeek += 7;
      }
      value = 52;
      int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;
      if ((magic == 6) ||
          (date.isLeapYear() && (magic == 5 || magic == 12))) {
          value++;
      }
        }
        break;
    }

    if (jc == this) {
        jc = (JapaneseImperialCalendar) jc.clone();
    }
    int max = getActualMaximum(DAY_OF_YEAR);
    jc.set(DAY_OF_YEAR, max);
    value = jc.get(WEEK_OF_YEAR);
    if (value == 1 && max > 7) {
        jc.add(WEEK_OF_YEAR, -1);
        value = jc.get(WEEK_OF_YEAR);
    }
      }
      break;

        case WEEK_OF_MONTH:
      {
    LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE,
                      getZone());
    if (!(date.getEra() == jd.getEra() && date.getYear() == jd.getYear())) {
        CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
        d.setDate(date.getNormalizedYear(), date.getMonth(), 1);
        int dayOfWeek = gcal.getDayOfWeek(d);
        int monthLength = gcal.getMonthLength(d);
        dayOfWeek -= getFirstDayOfWeek();
        if (dayOfWeek < 0) {
      dayOfWeek += 7;
        }
        int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week
        value = 3;
        if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {
      value++;
        }
        monthLength -= nDaysFirstWeek + 7 * 3;
        if (monthLength > 0) {
      value++;
      if (monthLength > 7) {
          value++;
      }
        }
    } else {
        long fd = jcal.getFixedDate(jd);
        long month1 = fd - jd.getDayOfMonth() + 1;
        value = getWeekNumber(month1, fd);
    }
      }
      break;

        case DAY_OF_WEEK_IN_MONTH:
      {
    int ndays, dow1;
    int dow = date.getDayOfWeek();
    BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
    ndays = jcal.getMonthLength(d);
    d.setDayOfMonth(1);
    jcal.normalize(d);
    dow1 = d.getDayOfWeek();
    int x = dow - dow1;
    if (x < 0) {
        x += 7;
    }
    ndays -= x;
    value = (ndays + 6) / 7;
      }
      break;

  case YEAR:
      {
    CalendarDate jd = jcal.getCalendarDate(jc.getTimeInMillis(), getZone());
    CalendarDate d;
    int eraIndex = getEraIndex(date);
    if (eraIndex == eras.length - 1) {
        d = jcal.getCalendarDate(Long.MAX_VALUE, getZone());
        value = d.getYear();
        // Use an equivalent year for the
        // getYearOffsetInMillis call to avoid overflow.
        if (value > 400) {
      jd.setYear(value - 400);
        }
    } else {
        d = jcal.getCalendarDate(eras[eraIndex + 1].getSince(getZone()) - 1,
               getZone());
        value = d.getYear();
        // Use the same year as d.getYear() to be
        // consistent with leap and common years.
        jd.setYear(value);
    }
    jcal.normalize(jd);
View Full Code Here

      long fixedDateJan1;
      if (transitionYear) {
    fixedDateJan1 = getFixedDateJan1(jdate, fixedDate);
    dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
      } else if (normalizedYear == MIN_VALUES[YEAR]) {
    CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
    fixedDateJan1 = jcal.getFixedDate(dx);
    dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
      } else {
    dayOfYear = (int) jcal.getDayOfYear(jdate);
    fixedDateJan1 = fixedDate - dayOfYear + 1;
      }
      long fixedDateMonth1 = transitionYear ?
    getFixedDateMonth1(jdate, fixedDate) : fixedDate - dayOfMonth + 1;

      internalSet(DAY_OF_YEAR, dayOfYear);
      internalSet(DAY_OF_WEEK_IN_MONTH, (dayOfMonth - 1) / 7 + 1);

      int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);

      // The spec is to calculate WEEK_OF_YEAR in the
      // ISO8601-style. This creates problems, though.
      if (weekOfYear == 0) {
    // If the date belongs to the last week of the
    // previous year, use the week number of "12/31" of
    // the "previous" year. Again, if the previous year is
    // a transition year, we need to take care of it.
    // Usually the previous day of the first day of a year
    // is December 31, which is not always true in the
    // Japanese imperial calendar system.
    long fixedDec31 = fixedDateJan1 - 1;
    long prevJan1;
    LocalGregorianCalendar.Date d = getCalendarDate(fixedDec31);
    if (!(transitionYear || isTransitionYear(d.getNormalizedYear()))) {
        prevJan1 = fixedDateJan1 - 365;
        if (d.isLeapYear()) {
      --prevJan1;
        }
    } else if (transitionYear) {
        if (jdate.getYear() == 1) {
      // As of Heisei (since Meiji) there's no case
      // that there are multiple transitions in a
      // year.  Historically there was such
      // case. There might be such case again in the
      // future.
      if (era > HEISEI) {
          CalendarDate pd = eras[era - 1].getSinceDate();
          if (normalizedYear == pd.getYear()) {
        d.setMonth(pd.getMonth()).setDayOfMonth(pd.getDayOfMonth());
          }
      } else {
          d.setMonth(jcal.JANUARY).setDayOfMonth(1);
      }
      jcal.normalize(d);
      prevJan1 = jcal.getFixedDate(d);
        } else {
      prevJan1 = fixedDateJan1 - 365;
      if (d.isLeapYear()) {
          --prevJan1;
      }
        }
    } else {
        CalendarDate cd = eras[getEraIndex(jdate)].getSinceDate();
        d.setMonth(cd.getMonth()).setDayOfMonth(cd.getDayOfMonth());
        jcal.normalize(d);
        prevJan1 = jcal.getFixedDate(d);
    }
    weekOfYear = getWeekNumber(prevJan1, fixedDec31);
      } else {
    if (!transitionYear) {
        // Regular years
        if (weekOfYear >= 52) {
      long nextJan1 = fixedDateJan1 + 365;
      if (jdate.isLeapYear()) {
          nextJan1++;
      }
      long nextJan1st = jcal.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
                    getFirstDayOfWeek());
      int ndays = (int)(nextJan1st - nextJan1);
      if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
          // The first days forms a week in which the date is included.
          weekOfYear = 1;
      }
        }
    } else {
        LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone();
        long nextJan1;
        if (jdate.getYear() == 1) {
      d.addYear(+1);
      d.setMonth(jcal.JANUARY).setDayOfMonth(1);
      nextJan1 = jcal.getFixedDate(d);
        } else {
      int nextEraIndex = getEraIndex(d) + 1;
      CalendarDate cd = eras[nextEraIndex].getSinceDate();
      d.setEra(eras[nextEraIndex]);
      d.setDate(1, cd.getMonth(), cd.getDayOfMonth());
      jcal.normalize(d);
      nextJan1 = jcal.getFixedDate(d);
        }
        long nextJan1st = jcal.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
                      getFirstDayOfWeek());
View Full Code Here

                year += CalendarUtils.floorDivide(month, 12, rem);
                month = rem[0];
            }
  } else {
      if (year == 1 && era != 0) {
    CalendarDate d = eras[era].getSinceDate();
    month = d.getMonth() - 1;
    firstDayOfMonth = d.getDayOfMonth();
      }
  }

  // Adjust the base date if year is the minimum value.
  if (year == MIN_VALUES[YEAR]) {
      CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
      int m = dx.getMonth() - 1;
      if (month < m)
    month = m;
      if (month == m)
    firstDayOfMonth = dx.getDayOfMonth();
  }

  LocalGregorianCalendar.Date date = jcal.newCalendarDate(TimeZone.NO_TIMEZONE);
  date.setEra(era > 0 ? eras[era] : null);
  date.setDate(year, month + 1, firstDayOfMonth);
View Full Code Here

     */
    private final long getFixedDateJan1(LocalGregorianCalendar.Date date, long fixedDate) {
  Era era = date.getEra();
  if (date.getEra() != null && date.getYear() == 1) {
      for (int eraIndex = getEraIndex(date); eraIndex > 0; eraIndex--) {
    CalendarDate d = eras[eraIndex].getSinceDate();
    long fd = gcal.getFixedDate(d);
    // There might be multiple era transitions in a year.
    if (fd > fixedDate) {
        continue;
    }
    return fd;
      }
  }
  CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
  d.setDate(date.getNormalizedYear(), gcal.JANUARY, 1);
  return gcal.getFixedDate(d);
    }
View Full Code Here

    private final int actualMonthLength() {
  int length = jcal.getMonthLength(jdate);
  int eraIndex = getTransitionEraIndex(jdate);
  if (eraIndex == -1) {
      long transitionFixedDate = sinceFixedDates[eraIndex];
      CalendarDate d = eras[eraIndex].getSinceDate();
      if (transitionFixedDate <= cachedFixedDate) {
    length -= d.getDayOfMonth() - 1;
      } else {
    length = d.getDayOfMonth() - 1;
      }
  }
  return length;
    }
View Full Code Here

TOP

Related Classes of sun.util.calendar.CalendarDate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.