Search in sources :

Example 26 with BaseCalendar

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

the class Date method UTC.

/**
     * Determines the date and time based on the arguments. The
     * arguments are interpreted as a year, month, day of the month,
     * hour of the day, minute within the hour, and second within the
     * minute, exactly as for the <tt>Date</tt> constructor with six
     * arguments, except that the arguments are interpreted relative
     * to UTC rather than to the local time zone. The time indicated is
     * returned represented as the distance, measured in milliseconds,
     * of that time from the epoch (00:00:00 GMT on January 1, 1970).
     *
     * @param   year    the year minus 1900.
     * @param   month   the month between 0-11.
     * @param   date    the day of the month between 1-31.
     * @param   hrs     the hours between 0-23.
     * @param   min     the minutes between 0-59.
     * @param   sec     the seconds between 0-59.
     * @return  the number of milliseconds since January 1, 1970, 00:00:00 GMT for
     *          the date and time specified by the arguments.
     * @see     java.util.Calendar
     * @deprecated As of JDK version 1.1,
     * replaced by <code>Calendar.set(year + 1900, month, date,
     * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
     * month, date, hrs, min, sec)</code>, using a UTC
     * <code>TimeZone</code>, followed by <code>Calendar.getTime().getTime()</code>.
     */
@Deprecated
public static long UTC(int year, int month, int date, int hrs, int min, int sec) {
    int y = year + 1900;
    // month is 0-based. So we have to normalize month to support Long.MAX_VALUE.
    if (month >= 12) {
        y += month / 12;
        month %= 12;
    } else if (month < 0) {
        y += CalendarUtils.floorDivide(month, 12);
        month = CalendarUtils.mod(month, 12);
    }
    int m = month + 1;
    BaseCalendar cal = getCalendarSystem(y);
    BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null);
    udate.setNormalizedDate(y, m, date).setTimeOfDay(hrs, min, sec, 0);
    // Use a Date instance to perform normalization. Its fastTime
    // is the UTC value after the normalization.
    Date d = new Date(0);
    d.normalize(udate);
    return d.fastTime;
}
Also used : BaseCalendar(sun.util.calendar.BaseCalendar) CalendarDate(sun.util.calendar.CalendarDate) LocalDate(java.time.LocalDate)

Example 27 with BaseCalendar

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

the class Date method normalize.

// fastTime and the returned data are in sync upon return.
private final BaseCalendar.Date normalize(BaseCalendar.Date date) {
    int y = date.getNormalizedYear();
    int m = date.getMonth();
    int d = date.getDayOfMonth();
    int hh = date.getHours();
    int mm = date.getMinutes();
    int ss = date.getSeconds();
    int ms = date.getMillis();
    TimeZone tz = date.getZone();
    // transition here.
    if (y == 1582 || y > 280000000 || y < -280000000) {
        if (tz == null) {
            tz = TimeZone.getTimeZone("GMT");
        }
        GregorianCalendar gc = new GregorianCalendar(tz);
        gc.clear();
        gc.set(GregorianCalendar.MILLISECOND, ms);
        gc.set(y, m - 1, d, hh, mm, ss);
        fastTime = gc.getTimeInMillis();
        BaseCalendar cal = getCalendarSystem(fastTime);
        date = (BaseCalendar.Date) cal.getCalendarDate(fastTime, tz);
        return date;
    }
    BaseCalendar cal = getCalendarSystem(y);
    if (cal != getCalendarSystem(date)) {
        date = (BaseCalendar.Date) cal.newCalendarDate(tz);
        date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
    }
    // Perform the GregorianCalendar-style normalization.
    fastTime = cal.getTime(date);
    // In case the normalized date requires the other calendar
    // system, we need to recalculate it using the other one.
    BaseCalendar ncal = getCalendarSystem(fastTime);
    if (ncal != cal) {
        date = (BaseCalendar.Date) ncal.newCalendarDate(tz);
        date.setNormalizedDate(y, m, d).setTimeOfDay(hh, mm, ss, ms);
        fastTime = ncal.getTime(date);
    }
    return date;
}
Also used : BaseCalendar(sun.util.calendar.BaseCalendar)

Example 28 with BaseCalendar

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

the class SimpleTimeZone method getOffset.

/**
     * Returns the difference in milliseconds between local time and
     * UTC, taking into account both the raw offset and the effect of
     * daylight saving, for the specified date and time.  This method
     * assumes that the start and end month are distinct.  It also
     * uses a default {@link GregorianCalendar} object as its
     * underlying calendar, such as for determining leap years.  Do
     * not use the result of this method with a calendar other than a
     * default <code>GregorianCalendar</code>.
     *
     * <p><em>Note:  In general, clients should use
     * <code>Calendar.get(ZONE_OFFSET) + Calendar.get(DST_OFFSET)</code>
     * instead of calling this method.</em>
     *
     * @param era       The era of the given date.
     * @param year      The year in the given date.
     * @param month     The month in the given date. Month is 0-based. e.g.,
     *                  0 for January.
     * @param day       The day-in-month of the given date.
     * @param dayOfWeek The day-of-week of the given date.
     * @param millis    The milliseconds in day in <em>standard</em> local time.
     * @return          The milliseconds to add to UTC to get local time.
     * @exception       IllegalArgumentException the <code>era</code>,
     *                  <code>month</code>, <code>day</code>, <code>dayOfWeek</code>,
     *                  or <code>millis</code> parameters are out of range
     */
public int getOffset(int era, int year, int month, int day, int dayOfWeek, int millis) {
    if (era != GregorianCalendar.AD && era != GregorianCalendar.BC) {
        throw new IllegalArgumentException("Illegal era " + era);
    }
    int y = year;
    if (era == GregorianCalendar.BC) {
        // adjust y with the GregorianCalendar-style year numbering.
        y = 1 - y;
    }
    // can't be supported by the Java time system.
    if (y >= 292278994) {
        y = 2800 + y % 2800;
    } else if (y <= -292269054) {
        // y %= 28 also produces an equivalent year, but positive
        // year numbers would be convenient to use the UNIX cal
        // command.
        y = (int) CalendarUtils.mod((long) y, 28);
    }
    // convert year to its 1-based month value
    int m = month + 1;
    // First, calculate time as a Gregorian date.
    BaseCalendar cal = gcal;
    BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
    cdate.setDate(y, m, day);
    // normalize cdate
    long time = cal.getTime(cdate);
    // UTC time
    time += millis - rawOffset;
    // style year numbering (..., -1, 0 (BCE 1), 1, 2, ...).
    if (time < GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER) {
        cal = (BaseCalendar) CalendarSystem.forName("julian");
        cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
        cdate.setNormalizedDate(y, m, day);
        time = cal.getTime(cdate) + millis - rawOffset;
    }
    if ((cdate.getNormalizedYear() != y) || (cdate.getMonth() != m) || (cdate.getDayOfMonth() != day) || // compatibility.
    (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY) || (millis < 0 || millis >= (24 * 60 * 60 * 1000))) {
        throw new IllegalArgumentException();
    }
    if (!useDaylight || year < startYear || era != GregorianCalendar.CE) {
        return rawOffset;
    }
    return getOffset(cal, cdate, y, time);
}
Also used : BaseCalendar(sun.util.calendar.BaseCalendar)

Example 29 with BaseCalendar

use of sun.util.calendar.BaseCalendar in project checker-framework by typetools.

the class Date method normalize.

private final BaseCalendar.Date normalize() {
    if (cdate == null) {
        BaseCalendar cal = getCalendarSystem(fastTime);
        cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime, TimeZone.getDefaultRef());
        return cdate;
    }
    // required for the compatible behavior.
    if (!cdate.isNormalized()) {
        cdate = normalize(cdate);
    }
    // If the default TimeZone has changed, then recalculate the
    // fields with the new TimeZone.
    TimeZone tz = TimeZone.getDefaultRef();
    if (tz != cdate.getZone()) {
        cdate.setZone(tz);
        CalendarSystem cal = getCalendarSystem(cdate);
        cal.getCalendarDate(fastTime, cdate);
    }
    return cdate;
}
Also used : BaseCalendar(sun.util.calendar.BaseCalendar) CalendarSystem(sun.util.calendar.CalendarSystem)

Example 30 with BaseCalendar

use of sun.util.calendar.BaseCalendar in project checker-framework by typetools.

the class Date method UTC.

/**
 * Determines the date and time based on the arguments. The
 * arguments are interpreted as a year, month, day of the month,
 * hour of the day, minute within the hour, and second within the
 * minute, exactly as for the <tt>Date</tt> constructor with six
 * arguments, except that the arguments are interpreted relative
 * to UTC rather than to the local time zone. The time indicated is
 * returned represented as the distance, measured in milliseconds,
 * of that time from the epoch (00:00:00 GMT on January 1, 1970).
 *
 * @param   year    the year minus 1900.
 * @param   month   the month between 0-11.
 * @param   date    the day of the month between 1-31.
 * @param   hrs     the hours between 0-23.
 * @param   min     the minutes between 0-59.
 * @param   sec     the seconds between 0-59.
 * @return  the number of milliseconds since January 1, 1970, 00:00:00 GMT for
 *          the date and time specified by the arguments.
 * @see     java.util.Calendar
 * @deprecated As of JDK version 1.1,
 * replaced by <code>Calendar.set(year + 1900, month, date,
 * hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,
 * month, date, hrs, min, sec)</code>, using a UTC
 * <code>TimeZone</code>, followed by <code>Calendar.getTime().getTime()</code>.
 */
@Deprecated
public static long UTC(int year, int month, int date, int hrs, int min, int sec) {
    int y = year + 1900;
    // month is 0-based. So we have to normalize month to support Long.MAX_VALUE.
    if (month >= 12) {
        y += month / 12;
        month %= 12;
    } else if (month < 0) {
        y += CalendarUtils.floorDivide(month, 12);
        month = CalendarUtils.mod(month, 12);
    }
    int m = month + 1;
    BaseCalendar cal = getCalendarSystem(y);
    BaseCalendar.Date udate = (BaseCalendar.Date) cal.newCalendarDate(null);
    udate.setNormalizedDate(y, m, date).setTimeOfDay(hrs, min, sec, 0);
    // Use a Date instance to perform normalization. Its fastTime
    // is the UTC value after the normalization.
    Date d = new Date(0);
    d.normalize(udate);
    return d.fastTime;
}
Also used : BaseCalendar(sun.util.calendar.BaseCalendar) CalendarDate(sun.util.calendar.CalendarDate)

Aggregations

BaseCalendar (sun.util.calendar.BaseCalendar)44 CalendarDate (sun.util.calendar.CalendarDate)20 CalendarSystem (sun.util.calendar.CalendarSystem)8 LocalDate (java.time.LocalDate)6 Era (sun.util.calendar.Era)4 ZoneInfo (sun.util.calendar.ZoneInfo)3 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)1