Javaクラスのソースコードと日付ツールのカレンダー

Javaクラスのソースコードと日付ツールのカレンダー

Calendarクラスの定義

public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {

    public final static int ERA = 0;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * year. This is a calendar-specific value; see subclass documentation.
     */
    public final static int YEAR = 1;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * month. This is a calendar-specific value. The first month of
     * the year in the Gregorian and Julian calendars is
     * <code>JANUARY</code> which is 0; the last depends on the number
     * of months in a year.
     *
     * @see #JANUARY
     * @see #FEBRUARY
     * @see #MARCH
     * @see #APRIL
     * @see #MAY
     * @see #JUNE
     * @see #JULY
     * @see #AUGUST
     * @see #SEPTEMBER
     * @see #OCTOBER
     * @see #NOVEMBER
     * @see #DECEMBER
     * @see #UNDECIMBER
     */
    public final static int MONTH = 2;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * week number within the current year.  The first week of the year, as
     * defined by <code>getFirstDayOfWeek()</code> and
     * <code>getMinimalDaysInFirstWeek()</code>, has value 1.  Subclasses define
     * the value of <code>WEEK_OF_YEAR</code> for days before the first week of
     * the year.
     *
     * @see #getFirstDayOfWeek
     * @see #getMinimalDaysInFirstWeek
     */
    public final static int WEEK_OF_YEAR = 3;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * week number within the current month.  The first week of the month, as
     * defined by <code>getFirstDayOfWeek()</code> and
     * <code>getMinimalDaysInFirstWeek()</code>, has value 1.  Subclasses define
     * the value of <code>WEEK_OF_MONTH</code> for days before the first week of
     * the month.
     *
     * @see #getFirstDayOfWeek
     * @see #getMinimalDaysInFirstWeek
     */
    public final static int WEEK_OF_MONTH = 4;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * day of the month. This is a synonym for <code>DAY_OF_MONTH</code>.
     * The first day of the month has value 1.
     *
     * @see #DAY_OF_MONTH
     */
    public final static int DATE = 5;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * day of the month. This is a synonym for <code>DATE</code>.
     * The first day of the month has value 1.
     *
     * @see #DATE
     */
    public final static int DAY_OF_MONTH = 5;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the day
     * number within the current year.  The first day of the year has value 1.
     */
    public final static int DAY_OF_YEAR = 6;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the day
     * of the week.  This field takes values <code>SUNDAY</code>,
     * <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
     * <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
     *
     * @see #SUNDAY
     * @see #MONDAY
     * @see #TUESDAY
     * @see #WEDNESDAY
     * @see #THURSDAY
     * @see #FRIDAY
     * @see #SATURDAY
     */
    public final static int DAY_OF_WEEK = 7;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * ordinal number of the day of the week within the current month. Together
     * with the <code>DAY_OF_WEEK</code> field, this uniquely specifies a day
     * within a month.  Unlike <code>WEEK_OF_MONTH</code> and
     * <code>WEEK_OF_YEAR</code>, this field's value does <em>not</em> depend on
     * <code>getFirstDayOfWeek()</code> or
     * <code>getMinimalDaysInFirstWeek()</code>.  <code>DAY_OF_MONTH 1</code>
     * through <code>7</code> always correspond to <code>DAY_OF_WEEK_IN_MONTH
     * 1</code>; <code>8</code> through <code>14</code> correspond to
     * <code>DAY_OF_WEEK_IN_MONTH 2</code>, and so on.
     * <code>DAY_OF_WEEK_IN_MONTH 0</code> indicates the week before
     * <code>DAY_OF_WEEK_IN_MONTH 1</code>.  Negative values count back from the
     * end of the month, so the last Sunday of a month is specified as
     * <code>DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1</code>.  Because
     * negative values count backward they will usually be aligned differently
     * within the month than positive values.  For example, if a month has 31
     * days, <code>DAY_OF_WEEK_IN_MONTH -1</code> will overlap
     * <code>DAY_OF_WEEK_IN_MONTH 5</code> and the end of <code>4</code>.
     *
     * @see #DAY_OF_WEEK
     * @see #WEEK_OF_MONTH
     */
    public final static int DAY_OF_WEEK_IN_MONTH = 8;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating
     * whether the <code>HOUR</code> is before or after noon.
     * E.g., at 10:04:15.250 PM the <code>AM_PM</code> is <code>PM</code>.
     *
     * @see #AM
     * @see #PM
     * @see #HOUR
     */
    public final static int AM_PM = 9;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * hour of the morning or afternoon. <code>HOUR</code> is used for the
     * 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12.
     * E.g., at 10:04:15.250 PM the <code>HOUR</code> is 10.
     *
     * @see #AM_PM
     * @see #HOUR_OF_DAY
     */
    public final static int HOUR = 10;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * hour of the day. <code>HOUR_OF_DAY</code> is used for the 24-hour clock.
     * E.g., at 10:04:15.250 PM the <code>HOUR_OF_DAY</code> is 22.
     *
     * @see #HOUR
     */
    public final static int HOUR_OF_DAY = 11;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * minute within the hour.
     * E.g., at 10:04:15.250 PM the <code>MINUTE</code> is 4.
     */
    public final static int MINUTE = 12;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * second within the minute.
     * E.g., at 10:04:15.250 PM the <code>SECOND</code> is 15.
     */
    public final static int SECOND = 13;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * millisecond within the second.
     * E.g., at 10:04:15.250 PM the <code>MILLISECOND</code> is 250.
     */
    public final static int MILLISECOND = 14;

  @SuppressWarnings("ProtectedField")
    protected int           fields[];

    @SuppressWarnings("ProtectedField")
    protected boolean       isSet[];

    transient private int   stamp[];

    @SuppressWarnings("ProtectedField")
    protected long          time;

    @SuppressWarnings("ProtectedField")
    protected boolean       isTimeSet;

    @SuppressWarnings("ProtectedField")
    protected boolean       areFieldsSet;

    transient boolean       areAllFieldsSet;

    private boolean         lenient = true;

    private TimeZone        zone;

    transient private boolean sharedZone = false;

    private int             firstDayOfWeek;

    private int             minimalDaysInFirstWeek;

    private static final ConcurrentMap<Locale, int[]> cachedLocaleData
        = new ConcurrentHashMap<>(3);

    protected Calendar()
    {
        this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
        sharedZone = true;
    }

    protected Calendar(TimeZone zone, Locale aLocale)
    {
        fields = new int[FIELD_COUNT];
        isSet = new boolean[FIELD_COUNT];
        stamp = new int[FIELD_COUNT];

        this.zone = zone;
        setWeekCountData(aLocale);
    }

    public static Calendar getInstance()
    {
        return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
    }

    public static Calendar getInstance(TimeZone zone)
    {
        return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT));
    }

    public static Calendar getInstance(Locale aLocale)
    {
        return createCalendar(TimeZone.getDefault(), aLocale);
    }

    public static Calendar getInstance(TimeZone zone,
                                       Locale aLocale)
    {
        return createCalendar(zone, aLocale);
    }

    private static Calendar createCalendar(TimeZone zone,
                                           Locale aLocale)
    {
        CalendarProvider provider =
            LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
                                 .getCalendarProvider();
        if (provider != null) {
            try {
                return provider.getInstance(zone, aLocale);
            } catch (IllegalArgumentException iae) {
                // fall back to the default instantiation
            }
        }

        Calendar cal = null;

        if (aLocale.hasExtensions()) {
            String caltype = aLocale.getUnicodeLocaleType("ca");
            if (caltype != null) {
                switch (caltype) {
                case "buddhist":
                cal = new BuddhistCalendar(zone, aLocale);
                    break;
                case "japanese":
                    cal = new JapaneseImperialCalendar(zone, aLocale);
                    break;
                case "gregory":
                    cal = new GregorianCalendar(zone, aLocale);
                    break;
                }
            }
        }
        if (cal == null) {
            // If no known calendar type is explicitly specified,
            // perform the traditional way to create a Calendar:
            // create a BuddhistCalendar for th_TH locale,
            // a JapaneseImperialCalendar for ja_JP_JP locale, or
            // a GregorianCalendar for any other locales.
            // NOTE: The language, country and variant strings are interned.
            if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
                cal = new BuddhistCalendar(zone, aLocale);
            } else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
                       && aLocale.getCountry() == "JP") {
                cal = new JapaneseImperialCalendar(zone, aLocale);
            } else {
                cal = new GregorianCalendar(zone, aLocale);
            }
        }
        return cal;
    }

    public static synchronized Locale[] getAvailableLocales()
    {
        return DateFormat.getAvailableLocales();
    }

    protected abstract void computeTime();

    protected abstract void computeFields();

    public final Date getTime() {
        return new Date(getTimeInMillis());
    }

    public final void setTime(Date date) {
        setTimeInMillis(date.getTime());
    }

    public long getTimeInMillis() {
        if (!isTimeSet) {
            updateTime();
        }
        return time;
    }

    public void setTimeInMillis(long millis) {
        // If we don't need to recalculate the calendar field values,
        // do nothing.
        if (time == millis && isTimeSet && areFieldsSet && areAllFieldsSet
            && (zone instanceof ZoneInfo) && !((ZoneInfo)zone).isDirty()) {
            return;
        }
        time = millis;
        isTimeSet = true;
        areFieldsSet = false;
        computeFields();
        areAllFieldsSet = areFieldsSet = true;
    }

    public int get(int field)
    {
        complete();
        return internalGet(field);
    }

    protected final int internalGet(int field)
    {
        return fields[field];
    }

    final void internalSet(int field, int value)
    {
        fields[field] = value;
    }

    public void set(int field, int value)
    {
        // If the fields are partially normalized, calculate all the
        // fields before changing any fields.
        if (areFieldsSet && !areAllFieldsSet) {
            computeFields();
        }
        internalSet(field, value);
        isTimeSet = false;
        areFieldsSet = false;
        isSet[field] = true;
        stamp[field] = nextStamp++;
        if (nextStamp == Integer.MAX_VALUE) {
            adjustStamp();
        }
    }
 
    public final void set(int year, int month, int date)
    {
        set(YEAR, year);
        set(MONTH, month);
        set(DATE, date);
    }

    public final void set(int year, int month, int date, int hourOfDay, int minute)
    {
        set(YEAR, year);
        set(MONTH, month);
        set(DATE, date);
        set(HOUR_OF_DAY, hourOfDay);
        set(MINUTE, minute);
    }

    public final void set(int year, int month, int date, int hourOfDay, int minute,
                          int second)
    {
        set(YEAR, year);
        set(MONTH, month);
        set(DATE, date);
        set(HOUR_OF_DAY, hourOfDay);
        set(MINUTE, minute);
        set(SECOND, second);
    }

    public final void clear()
    {
        for (int i = 0; i < fields.length; ) {
            stamp[i] = fields[i] = 0; // UNSET == 0
            isSet[i++] = false;
        }
        areAllFieldsSet = areFieldsSet = false;
        isTimeSet = false;
    }

    public final void clear(int field)
    {
        fields[field] = 0;
        stamp[field] = UNSET;
        isSet[field] = false;

        areAllFieldsSet = areFieldsSet = false;
        isTimeSet = false;
    }

    public final boolean isSet(int field)
    {
        return stamp[field] != UNSET;
    }

   @Override
    public String toString() {
        // NOTE: BuddhistCalendar.toString() interprets the string
        // produced by this method so that the Gregorian year number
        // is substituted by its B.E. year value. It relies on
        // "...,YEAR=<year>,..." or "...,YEAR=?,...".
        StringBuilder buffer = new StringBuilder(800);
        buffer.append(getClass().getName()).append('[');
        appendValue(buffer, "time", isTimeSet, time);
        buffer.append(",areFieldsSet=").append(areFieldsSet);
        buffer.append(",areAllFieldsSet=").append(areAllFieldsSet);
        buffer.append(",lenient=").append(lenient);
        buffer.append(",zone=").append(zone);
        appendValue(buffer, ",firstDayOfWeek", true, (long) firstDayOfWeek);
        appendValue(buffer, ",minimalDaysInFirstWeek", true, (long) minimalDaysInFirstWeek);
        for (int i = 0; i < FIELD_COUNT; ++i) {
            buffer.append(',');
            appendValue(buffer, FIELD_NAME[i], isSet(i), (long) fields[i]);
        }
        buffer.append(']');
        return buffer.toString();
    }

    // =======================privates===============================

    private static void appendValue(StringBuilder sb, String item, boolean valid, long value) {
        sb.append(item).append('=');
        if (valid) {
            sb.append(value);
        } else {
            sb.append('?');
        }
    }

    private void setWeekCountData(Locale desiredLocale)
    {
        /* try to get the Locale data from the cache */
        int[] data = cachedLocaleData.get(desiredLocale);
        if (data == null) {  /* cache miss */
            data = new int[2];
            data[0] = CalendarDataUtility.retrieveFirstDayOfWeek(desiredLocale);
            data[1] = CalendarDataUtility.retrieveMinimalDaysInFirstWeek(desiredLocale);
            cachedLocaleData.putIfAbsent(desiredLocale, data);
        }
        firstDayOfWeek = data[0];
        minimalDaysInFirstWeek = data[1];
    }

    private void updateTime() {
        computeTime();
        // The areFieldsSet and areAllFieldsSet values are no longer
        // controlled here (as of 1.5).
        isTimeSet = true;
    }

    private int compareTo(long t) {
        long thisTime = getMillisOf(this);
        return (thisTime > t) ? 1 : (thisTime == t) ? 0 : -1;
    }

    private static long getMillisOf(Calendar calendar) {
        if (calendar.isTimeSet) {
            return calendar.time;
        }
        Calendar cal = (Calendar) calendar.clone();
        cal.setLenient(true);
        return cal.getTimeInMillis();
    }

    private void adjustStamp() {
        int max = MINIMUM_USER_STAMP;
        int newStamp = MINIMUM_USER_STAMP;

        for (;;) {
            int min = Integer.MAX_VALUE;
            for (int i = 0; i < stamp.length; i++) {
                int v = stamp[i];
                if (v >= newStamp && min > v) {
                    min = v;
                }
                if (max < v) {
                    max = v;
                }
            }
            if (max != min && min == Integer.MAX_VALUE) {
                break;
            }
            for (int i = 0; i < stamp.length; i++) {
                if (stamp[i] == min) {
                    stamp[i] = newStamp;
                }
            }
            newStamp++;
            if (min == max) {
                break;
            }
        }
        nextStamp = newStamp;
    }

    private void invalidateWeekFields()
    {
        if (stamp[WEEK_OF_MONTH] != COMPUTED &&
            stamp[WEEK_OF_YEAR] != COMPUTED) {
            return;
        }

        // We have to check the new values of these fields after changing
        // firstDayOfWeek and/or minimalDaysInFirstWeek. If the field values
        // have been changed, then set the new values. (4822110)
        Calendar cal = (Calendar) clone();
        cal.setLenient(true);
        cal.clear(WEEK_OF_MONTH);
        cal.clear(WEEK_OF_YEAR);

        if (stamp[WEEK_OF_MONTH] == COMPUTED) {
            int weekOfMonth = cal.get(WEEK_OF_MONTH);
            if (fields[WEEK_OF_MONTH] != weekOfMonth) {
                fields[WEEK_OF_MONTH] = weekOfMonth;
            }
        }

        if (stamp[WEEK_OF_YEAR] == COMPUTED) {
            int weekOfYear = cal.get(WEEK_OF_YEAR);
            if (fields[WEEK_OF_YEAR] != weekOfYear) {
                fields[WEEK_OF_YEAR] = weekOfYear;
            }
        }
    }

    private synchronized void writeObject(ObjectOutputStream stream)
         throws IOException
    {
        // Try to compute the time correctly, for the future (stream
        // version 2) in which we don't write out fields[] or isSet[].
        if (!isTimeSet) {
            try {
                updateTime();
            }
            catch (IllegalArgumentException e) {}
        }

        // If this Calendar has a ZoneInfo, save it and set a
        // SimpleTimeZone equivalent (as a single DST schedule) for
        // backward compatibility.
        TimeZone savedZone = null;
        if (zone instanceof ZoneInfo) {
            SimpleTimeZone stz = ((ZoneInfo)zone).getLastRuleInstance();
            if (stz == null) {
                stz = new SimpleTimeZone(zone.getRawOffset(), zone.getID());
            }
            savedZone = zone;
            zone = stz;
        }

        // Write out the 1.1 FCS object.
        stream.defaultWriteObject();

        // Write out the ZoneInfo object
        // 4802409: we write out even if it is null, a temporary workaround
        // the real fix for bug 4844924 in corba-iiop
        stream.writeObject(savedZone);
        if (savedZone != null) {
            zone = savedZone;
        }
    }

    private static class CalendarAccessControlContext {
        private static final AccessControlContext INSTANCE;
        static {
            RuntimePermission perm = new RuntimePermission("accessClassInPackage.sun.util.calendar");
            PermissionCollection perms = perm.newPermissionCollection();
            perms.add(perm);
            INSTANCE = new AccessControlContext(new ProtectionDomain[] {
                                                    new ProtectionDomain(null, perms)
                                                });
        }
        private CalendarAccessControlContext() {
        }
    }

    /**
     * Reconstitutes this object from a stream (i.e., deserialize it).
     */
    private void readObject(ObjectInputStream stream)
         throws IOException, ClassNotFoundException
    {
        final ObjectInputStream input = stream;
        input.defaultReadObject();

        stamp = new int[FIELD_COUNT];

        // Starting with version 2 (not implemented yet), we expect that
        // fields[], isSet[], isTimeSet, and areFieldsSet may not be
        // streamed out anymore.  We expect 'time' to be correct.
        if (serialVersionOnStream >= 2)
        {
            isTimeSet = true;
            if (fields == null) {
                fields = new int[FIELD_COUNT];
            }
            if (isSet == null) {
                isSet = new boolean[FIELD_COUNT];
            }
        }
        else if (serialVersionOnStream >= 0)
        {
            for (int i=0; i<FIELD_COUNT; ++i) {
                stamp[i] = isSet[i] ? COMPUTED : UNSET;
            }
        }

        serialVersionOnStream = currentSerialVersion;

        // If there's a ZoneInfo object, use it for zone.
        ZoneInfo zi = null;
        try {
            zi = AccessController.doPrivileged(
                    new PrivilegedExceptionAction<ZoneInfo>() {
                        @Override
                        public ZoneInfo run() throws Exception {
                            return (ZoneInfo) input.readObject();
                        }
                    },
                    CalendarAccessControlContext.INSTANCE);
        } catch (PrivilegedActionException pae) {
            Exception e = pae.getException();
            if (!(e instanceof OptionalDataException)) {
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else if (e instanceof IOException) {
                    throw (IOException) e;
                } else if (e instanceof ClassNotFoundException) {
                    throw (ClassNotFoundException) e;
                }
                throw new RuntimeException(e);
            }
        }
        if (zi != null) {
            zone = zi;
        }

        // If the deserialized object has a SimpleTimeZone, try to
        // replace it with a ZoneInfo equivalent (as of 1.4) in order
        // to be compatible with the SimpleTimeZone-based
        // implementation as much as possible.
        if (zone instanceof SimpleTimeZone) {
            String id = zone.getID();
            TimeZone tz = TimeZone.getTimeZone(id);
            if (tz != null && tz.hasSameRules(zone) && tz.getID().equals(id)) {
                zone = tz;
            }
        }
    }

    public final Instant toInstant() {
        return Instant.ofEpochMilli(getTimeInMillis());
    }
}

Javaは公式の使用カレンダーの使用を交換することをお勧めします。

カレンダーと日付の間で変換する無料、社債の転換は時間です。

Date型のオブジェクトであることができるgetTime()メソッドを使用してカレンダーは、ロング作成コンストラクタに第二型パラメータの使用の根底にある日付オブジェクトは、パラメータは、時間フィールドのロングカレンダ値に格納されています今回は、フィールドの値は、特定のカテゴリの割り当てで定義されています。

そのようなcomputeTimeてGregorianCalendar()内に実装、この方法の目的は、2つのモードカレンダーに関し、それは、後に導入され、時間値のフィールドに値を変換することです。

そして、あなたは、パラメータ値としてのsetTime(日付)Calendarオブジェクトへのカレンダーの日付オブジェクトのメソッドは、このメソッドのパラメータとしてDateオブジェクトを、setTimeInMillis(長いミリ)メソッド基礎となる呼び出しますdate.getTime()変換することができます、これは、次いで、再計算されますフィールド値フィールドに割り当てられる長い時間パラメータ値の底であろう。

変換のカレンダーと日付:

public class CalendarTest {
    public static void main(String[] args) {
        //Calendar--->Date
        Calendar c = Calendar.getInstance();
        Date d = c.getTime();
        //Date--->Calendar
        Date d1 = new Date();
        Calendar c1 = Calendar.getInstance();
        c1.setTime(d1);
        
        System.out.println(d);
        System.out.println(c1.get(Calendar.YEAR)+"年"+(c1.get(Calendar.MONTH)+1)+"月"+c1.get(Calendar.DATE)+"日");
    }
}

時間とフィールドのカレンダー

カレンダーの内容の2時間領域の記述がありますが、人は時間である、時間Calendarオブジェクト内のポイントを保存するために使用されている1970年1月1日00:00:00によると、ミリ秒単位の数を表し、他方はフィールドであり、それ配列は、それがコンテンツの表現ではなく、カレンダー内部で定義された最大静的定数フィールド。

通常の状況下ではなく、その時点で同じ点を発現し、それは同期せずにも可能であるが発生することを、同期されます。

  1. 最初は、フィールドが設定されていない、時間が無効です
  2. 時間が設定されている場合、フィールドのすべてが自動的に時間の同期ポイントに設定されています
  3. フィールドが別々に提供されている場合は、時間は自動的に期限切れになります

むしろ、我々はCalendar.getInstance()メソッドによって、新しいCalendarオブジェクトを取得するとき、それは、時間のポイントは、時間によって設定され、これはのSystem.currentTimeMillis(による時間の値である)を取得さを表します値フィールドのフィールドを示し、falseにareFieldsSet、isTimeSetは、最新の時間値(真)であること、真実である、カレンダーを定義した時点で、我々は時の値を再設定するので、(偽)古い、カレンダーそれが初めてここに変化(の時点を表し、ゼロからの同等は、変更と考えられ、その後、我々は再びタイムカレンダーが新しい値を設定することができ、時間のカレンダーポイントが再び変更されます、それは意志最新の時刻値が表す時点をポイント)が、フィールド内のこの時間は、時間の原点を表し、そしてその後、時刻フィールド同期の値ことを保証するために、すべてのフィールドの再計算値にcomputeFields()メソッドを呼び出しますそしてareFieldsSetとareAllFieldsSetがtrueに設定され、同時に、それはすべての分野の代表の時間値が(真)の最新であることを示します。上記Bの含有量は、実際に、我々は時間の値を変更するたびに自動的にトリガ再計算、時間に一貫性のあるポイントを確保するために、2つのドメインが記載されている(すなわち同期)。

しかし、私たちが作るために、フィールド内の行をフィールド場合は、セット(int型のフィールド、int型の値)によって個別に変化する第1の検証をトリガーする、真areFieldsSetとareAllFieldsSetが偽で、フィールドの一部のみが、最新の状況であることを示す、つまり、古い状況に属するフィールドの一部があり、このような状況は、フィールドの再計算をトリガーし、その後、偽、falseにareFieldsSetセットにisTimeSetセットは、ISSET [フィールド]が(現在のフィールドがtrueに設定されている)、trueに設定されますケースの下で、場合我々はgetTime()がtrueにisTimeSetセットを時間時間値のポイントを取得しisTimeSetの偽、再計算のトリガ時間は、計算の基礎は、値フィールドに基づいて行われるので、時間を表し、後に使用、同様に、我々はfalseの場合、それはまた、再計算時間をトリガし、その後areFieldsSetが偽、再計算のget(int型のフィールド)でフィールドの値を取得する際に、残りのフィールドがトリガさであることを確認します、isTimeSetが真であるかどうかを確認します。

重み計算時間がフィールドに基づいて、正確には、フィールドの一部のための基礎であり、フィールドが再計算に基づいてフィールドの一部であり、フィールドの固定された部分があると言うことができ、時間が密接に関連しています。

これらはすべて、外部的には、我々は単に私たちの直接の正しい値は、外部メソッドを介して取得していることを確認するように、内部に隠されているすべてが、呼び出すことができ、カレンダーの内部全体を実装するためのルールです。

public class CalendarTest {
    public static void main(String[] args) throws ParseException {
        System.out.println("-------初始情况-------");
        Calendar c = Calendar.getInstance();
        System.out.println(c.getTime());
        System.out.println(c.get(Calendar.DATE));
        System.out.println(c.get(Calendar.HOUR));
        System.out.println("-------重设置time-------");
        c.setTime(new SimpleDateFormat("yyyyMMdd").parse("20170501"));
        System.out.println(c.getTime());
        System.out.println(c.get(Calendar.DATE));
        System.out.println(c.get(Calendar.HOUR));
        System.out.println("-------重设置field-------");
        c.set(Calendar.MONTH, 4);
        System.out.println(c.getTime());
        System.out.println(c.get(Calendar.DATE));
        System.out.println(c.get(Calendar.HOUR));
        System.out.println("总结:time与field所代表时间点同步,所有的不同步全部在内部处理完成");
    }
}

結果:

-------初始情况-------
Sat Jul 08 13:08:34 CST 2017
8
1
-------重设置time-------
Mon May 01 00:00:00 CST 2017
1
0
-------重设置field-------
Mon May 01 00:00:00 CST 2017
1
0
总结:time与field所代表时间点同步,所有的不同步全部在内部处理完成

2つの解析モデルのカレンダー

  • 厳密:このモードでは、ユーザは、自動的に2月例えば1月32解決時に不規則なカレンダー値を正則化するために割り当てることができる1
  • 非厳密:自動的に不規則なパターンの入力を解析したが、不規則いったん入力されていない、異常が報告され

これはまた、フォールトトレランス、開口部のカレンダーと閉じて使用setLenient(ブール寛大)寛大を設定するための方法と呼ばれ、真のは、フォールトトレランス(デフォルト)、虚偽の表現を無効にこの機能を示しています。

例:

public class CalendarTest {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, 8);
        c.set(Calendar.DAY_OF_MONTH, 33);
        System.out.println(c.getTime()+"\n");
        c.setLenient(false);
        c.set(Calendar.MONTH, 8);
        c.set(Calendar.DAY_OF_MONTH, 33);
        System.out.println(c.getTime());
    }
}

結果:

Tue Oct 03 13:18:48 CST 2017

Exception in thread "main" java.lang.IllegalArgumentException: DAY_OF_MONTH
    at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2583)
    at java.util.Calendar.updateTime(Calendar.java:2606)
    at java.util.Calendar.getTimeInMillis(Calendar.java:1118)
    at java.util.Calendar.getTime(Calendar.java:1091)
    at JdkTest.main(JdkTest.java:87)

デフォルトでは、上記の例からわかるように、私たちは9月8日、来月号333に割り当てられた日付、10月3日のための出力の月のアサインカレンダー、これに準拠していますフォールトトレランス対処するためのルールのルールを入力しますが、フォールトトレランスを閉じた後、同じ割り当てが唯一の異常java.lang.IllegalArgumentExceptionが(不正な引数例外)を報告しました。

使用のカレンダー

例:

public class CalendarTest {
    public static void main(String[] args) throws ParseException {
        //通过SimpleDateFormat解析日期字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss.SSS");
        Date date = sdf.parse("20170502 13:33:23.433");
        //将Date格式日期转换成Calendar
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //获取时间值
        System.out.println(c.getTime());
        System.out.println("年份为"+c.get(Calendar.YEAR));
        System.out.println("月份为"+c.get(Calendar.MONTH));
        System.out.println("日期为"+c.get(Calendar.DATE));
        System.out.println("日期为"+c.get(Calendar.DAY_OF_MONTH));
        System.out.println("日期为"+c.get(Calendar.DAY_OF_WEEK));
        System.out.println("日期为"+c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println("日期为"+c.get(Calendar.DAY_OF_YEAR));
        System.out.println("时为"+c.get(Calendar.HOUR));
        System.out.println("时为"+c.get(Calendar.HOUR_OF_DAY));
        System.out.println("分为"+c.get(Calendar.MINUTE));
        System.out.println("秒为"+c.get(Calendar.SECOND));
        System.out.println("毫秒为"+c.get(Calendar.MILLISECOND));
        System.out.println("星期为"+c.get(Calendar.WEEK_OF_MONTH));
        System.out.println("星期为"+c.get(Calendar.WEEK_OF_YEAR));
        System.out.println("历型为"+c.get(Calendar.ERA));
        System.out.println("zone为"+c.get(Calendar.ZONE_OFFSET));
        //设置
        c.set(Calendar.MONTH, Calendar.APRIL);
        System.out.println("修改后月份为"+c.get(Calendar.MONTH));
        c.set(1999, 0, 23);
        System.out.println(c.getTime());
        c.set(2000, 1, 12, 13, 33, 14);
        System.out.println(c.getTime());
        c.set(2001, 2, 13, 14, 13);
        System.out.println(c.getTime());
        //运算
        System.out.println("-----运算-----");
        c.add(Calendar.YEAR, 12);
        System.out.println(c.getTime());
        c.add(Calendar.MONTH, -1);
        System.out.println(c.getTime());
        c.roll(Calendar.DATE, true);
        System.out.println(c.getTime());
        c.add(Calendar.DATE, 1);
        System.out.println(c.getTime());
        //roll与add运算对比
        c.set(2000, 1, 29);
        System.out.println(c.getTime());
        c.roll(Calendar.DATE, 1);
        System.out.println(c.getTime());
        c.set(2000, 1, 29);
        c.add(Calendar.DATE, 1);
        System.out.println(c.getTime());
    }
}

結果の実装:

Tue May 02 13:33:23 CST 2017
年份为2017
月份为4
日期为2
日期为2
日期为3
日期为1
日期为122
时为1
时为13
分为33
秒为23
毫秒为433
星期为1
星期为18
历型为1
zone为28800000
修改后月份为3
Sat Jan 23 13:33:23 CST 1999
Sat Feb 12 13:33:14 CST 2000
Tue Mar 13 14:13:14 CST 2001
-----运算-----
Wed Mar 13 14:13:14 CST 2013
Wed Feb 13 14:13:14 CST 2013
Thu Feb 14 14:13:14 CST 2013
Fri Feb 15 14:13:14 CST 2013
Tue Feb 29 14:13:14 CST 2000
Tue Feb 01 14:13:14 CST 2000
Wed Mar 01 14:13:14 CST 2000

上記の比較出力の最後の2行は、それが動作アドオンとロールのルールを変更するために見ることができるロールは、操作が衝撃を追加する大きなルールを(ここでは、大きなルールは月の変化を指す)、影響はありません、実際に異なっています。

 

 

 

 

 

公開された370元の記事 ウォン称賛88 ビュー290 000 +

おすすめ

転載: blog.csdn.net/qq_35029061/article/details/100523688