Search in sources :

Example 16 with CalendarDate

use of sun.util.calendar.CalendarDate in project jdk8u_jdk by JetBrains.

the class JapaneseImperialCalendar method getFixedDate.

/**
     * Computes the fixed date under either the Gregorian or the
     * Julian calendar, using the given year and the specified calendar fields.
     *
     * @param era era index
     * @param year the normalized year number, with 0 indicating the
     * year 1 BCE, -1 indicating 2 BCE, etc.
     * @param fieldMask the calendar fields to be used for the date calculation
     * @return the fixed date
     * @see Calendar#selectFields
     */
private long getFixedDate(int era, int year, int fieldMask) {
    int month = JANUARY;
    int firstDayOfMonth = 1;
    if (isFieldSet(fieldMask, MONTH)) {
        // No need to check if MONTH has been set (no isSet(MONTH)
        // call) since its unset value happens to be JANUARY (0).
        month = internalGet(MONTH);
        // If the month is out of range, adjust it into range.
        if (month > DECEMBER) {
            year += month / 12;
            month %= 12;
        } else if (month < JANUARY) {
            int[] rem = new int[1];
            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);
    jcal.normalize(date);
    // Get the fixed date since Jan 1, 1 (Gregorian). We are on
    // the first day of either `month' or January in 'year'.
    long fixedDate = jcal.getFixedDate(date);
    if (isFieldSet(fieldMask, MONTH)) {
        // Month-based calculations
        if (isFieldSet(fieldMask, DAY_OF_MONTH)) {
            // since the default value is the "first day".
            if (isSet(DAY_OF_MONTH)) {
                // To avoid underflow with DAY_OF_MONTH-firstDayOfMonth, add
                // DAY_OF_MONTH, then subtract firstDayOfMonth.
                fixedDate += internalGet(DAY_OF_MONTH);
                fixedDate -= firstDayOfMonth;
            }
        } else {
            if (isFieldSet(fieldMask, WEEK_OF_MONTH)) {
                long firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6, getFirstDayOfWeek());
                // move to the previous week.
                if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
                    firstDayOfWeek -= 7;
                }
                if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
                    firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, internalGet(DAY_OF_WEEK));
                }
                // In lenient mode, we treat days of the previous
                // months as a part of the specified
                // WEEK_OF_MONTH. See 4633646.
                fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1);
            } else {
                int dayOfWeek;
                if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
                    dayOfWeek = internalGet(DAY_OF_WEEK);
                } else {
                    dayOfWeek = getFirstDayOfWeek();
                }
                // We are basing this on the day-of-week-in-month.  The only
                // trickiness occurs if the day-of-week-in-month is
                // negative.
                int dowim;
                if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) {
                    dowim = internalGet(DAY_OF_WEEK_IN_MONTH);
                } else {
                    dowim = 1;
                }
                if (dowim >= 0) {
                    fixedDate = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1, dayOfWeek);
                } else {
                    // Go to the first day of the next week of
                    // the specified week boundary.
                    int lastDate = monthLength(month, year) + (7 * (dowim + 1));
                    // Then, get the day of week date on or before the last date.
                    fixedDate = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1, dayOfWeek);
                }
            }
        }
    } else {
        // We are on the first day of the year.
        if (isFieldSet(fieldMask, DAY_OF_YEAR)) {
            if (isTransitionYear(date.getNormalizedYear())) {
                fixedDate = getFixedDateJan1(date, fixedDate);
            }
            // Add the offset, then subtract 1. (Make sure to avoid underflow.)
            fixedDate += internalGet(DAY_OF_YEAR);
            fixedDate--;
        } else {
            long firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6, getFirstDayOfWeek());
            // to the previous week.
            if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
                firstDayOfWeek -= 7;
            }
            if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
                int dayOfWeek = internalGet(DAY_OF_WEEK);
                if (dayOfWeek != getFirstDayOfWeek()) {
                    firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6, dayOfWeek);
                }
            }
            fixedDate = firstDayOfWeek + 7 * ((long) internalGet(WEEK_OF_YEAR) - 1);
        }
    }
    return fixedDate;
}
Also used : CalendarDate(sun.util.calendar.CalendarDate) LocalGregorianCalendar(sun.util.calendar.LocalGregorianCalendar)

Example 17 with CalendarDate

use of sun.util.calendar.CalendarDate in project jdk8u_jdk by JetBrains.

the class JapaneseImperialCalendar method actualMonthLength.

private 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;
}
Also used : CalendarDate(sun.util.calendar.CalendarDate)

Aggregations

CalendarDate (sun.util.calendar.CalendarDate)17 BaseCalendar (sun.util.calendar.BaseCalendar)5 CalendarSystem (sun.util.calendar.CalendarSystem)4 LocalGregorianCalendar (sun.util.calendar.LocalGregorianCalendar)4 IOException (java.io.IOException)2 Date (java.util.Date)2 DateTimeException (java.time.DateTimeException)1 LocalDate (java.time.LocalDate)1 Era (sun.util.calendar.Era)1 ZoneInfo (sun.util.calendar.ZoneInfo)1