ソースの読み取り:新日ツアー

Javaの日付からの風

その日はYoumieは、ソースコードの日付を理解する必要が同僚に尋ねた、同僚は決してについて調べるために言いませんでした。プレイヤー日内の通常の日付ツールとして、Zhefanの答えは、おそらく満足されることはありません、我々は選手といくつかのインタビューをやろうとしている、私たちは再認識させるDateクラスのjava.utilパッケージには、それを提供します。

public static void main(String[] args) {
    Date date = new Date();
    System.out.println(date);
}

私たちは、印刷結果を見てみましょう

火曜1月14日夜02時51分54秒CST 2020

何
出力クラスDateは行うべきではないでしょうか。それは、我々は、詳細な日付のベテランを継続する必要があるようですので、髪のために、霧のその層を開きました。

霧に

 /**
     * Allocates a <code>Date</code> object and initializes it so that
     * it represents the time at which it was allocated, measured to the
     * nearest millisecond.
     *
     * @see     java.lang.System#currentTimeMillis()
     */
    public Date() {
        this(System.currentTimeMillis());
    }

    /**
     * Allocates a <code>Date</code> object and initializes it to
     * represent the specified number of milliseconds since the
     * standard base time known as "the epoch", namely January 1,
     * 1970, 00:00:00 GMT.
     *
     * @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.
     * @see     java.lang.System#currentTimeMillis()
     */
    public Date(long date) {
        fastTime = date;
    }

この時点で、我々はコンストラクタがのSystem.currentTimeMillis()システム時刻と呼ばれ、最終的にはfastTimeの割り当てに持っている日が終了されますが、日付自体は問題ありませんので、結果は、文字列のことである理由、それはSystem.out.printlnはある見ます()私たちが知らないことを行うれ、道路長い道のりのソースは、それを継続すると思われます。

	/**
     * Prints an Object and then terminate the line.  This method calls
     * at first String.valueOf(x) to get the printed object's string value,
     * then behaves as
     * though it invokes <code>{@link #print(String)}</code> and then
     * <code>{@link #println()}</code>.
     *
     * @param x  The <code>Object</code> to be printed.
     */
    public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }

真実は、私たちはString.valueOf(X)を見てみましょう、出てくるのに約思われるもの、それをやりました

	/**
     * Returns the string representation of the {@code Object} argument.
     *
     * @param   obj   an {@code Object}.
     * @return  if the argument is {@code null}, then a string equal to
     *          {@code "null"}; otherwise, the value of
     *          {@code obj.toString()} is returned.
     * @see     java.lang.Object#toString()
     */
    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
	/**
     * Converts this <code>Date</code> object to a <code>String</code>
     * of the form:
     * <blockquote><pre>
     * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
     * where:<ul>
     * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
     *     Thu, Fri, Sat</tt>).
     * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
     *     Jul, Aug, Sep, Oct, Nov, Dec</tt>).
     * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
     *     <tt>31</tt>), as two decimal digits.
     * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
     *     <tt>23</tt>), as two decimal digits.
     * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
     *     <tt>59</tt>), as two decimal digits.
     * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
     *     <tt>61</tt>, as two decimal digits.
     * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving
     *     time). Standard time zone abbreviations include those
     *     recognized by the method <tt>parse</tt>. If time zone
     *     information is not available, then <tt>zzz</tt> is empty -
     *     that is, it consists of no characters at all.
     * <li><tt>yyyy</tt> is the year, as four decimal digits.
     * </ul>
     *
     * @return  a string representation of this date.
     * @see     java.util.Date#toLocaleString()
     * @see     java.util.Date#toGMTString()
     */
    public String toString() {
        // "EEE MMM dd HH:mm:ss zzz yyyy";
        BaseCalendar.Date date = normalize();
        StringBuilder sb = new StringBuilder(28);
        int index = date.getDayOfWeek();
        if (index == BaseCalendar.SUNDAY) {
            index = 8;
        }
        convertToAbbr(sb, wtb[index]).append(' ');                        // EEE
        convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' ');  // MMM
        CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd

        CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':');   // HH
        CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
        CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
        TimeZone zi = date.getZone();
        if (zi != null) {
            sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz
        } else {
            sb.append("GMT");
        }
        sb.append(' ').append(date.getYear());  // yyyy
        return sb.toString();
    }

デバッグ

散乱雲は、オープン霧

あなたはクラスオブジェクトを印刷するとき真実を語ってこの時点で、自動的にtoStringメソッドを呼び出します、最終的な結果がありました。もちろん、同時に私たちの秘密の日付が、また、fastTimeから切っても切れないとヘルプCDATE 2人のパートナー。

    private transient long fastTime;

    /*
     * If cdate is null, then fastTime indicates the time in millis.
     * If cdate.isNormalized() is true, then fastTime and cdate are in
     * synch. Otherwise, fastTime is ignored, and cdate indicates the
     * time.
     */
    private transient BaseCalendar.Date cdate;

のBaseCalendarのsun.util.calendarから特筆ゲストスター。

public abstract class BaseCalendar extends AbstractCalendar {
    public static final int JANUARY = 1;
    public static final int FEBRUARY = 2;
    public static final int MARCH = 3;
    public static final int APRIL = 4;
    public static final int MAY = 5;
    public static final int JUNE = 6;
    public static final int JULY = 7;
    public static final int AUGUST = 8;
    public static final int SEPTEMBER = 9;
    public static final int OCTOBER = 10;
    public static final int NOVEMBER = 11;
    public static final int DECEMBER = 12;
    public static final int SUNDAY = 1;
    public static final int MONDAY = 2;
    public static final int TUESDAY = 3;
    public static final int WEDNESDAY = 4;
    public static final int THURSDAY = 5;
    public static final int FRIDAY = 6;
    public static final int SATURDAY = 7;
    private static final int BASE_YEAR = 1970;
    ...
    }

医療知識にしばしば知識も真である「一般的には悪くはない、痛みは無理がある」とは言うではなく、一般的な未知の、ことわざは、物理的および100黎明を通過します。

番外

私たちは、ほとんどこの瞬間言語からのすべての時間がカウントされ、それは、Javaだけではない、これが起こる理由は、道路のデバッグ最初の時間は1970年1月1日で見つかりました。JavaはUNIXシステムの起源、およびUNIXは、1970年1月1日の0:00時点の時代であると思うので。私たちが見るすべての結果に。

公開された13元の記事 ウォン称賛14 ビュー50000 +

おすすめ

転載: blog.csdn.net/qq_35764295/article/details/103971183