java基礎系列--Date類

來源:互聯網
上載者:User

標籤:先後   ase   off   val   方式   transient   tar   iso   tran   

原創作品,可以轉載,但是請標註出處地址:http://www.cnblogs.com/V1haoge/p/7126930.html

1、Date類概述

  Date類是從JDK1.1就開始存在的老類,其提供了針對日期進行操作的諸多方法,但其卻一直飽受詬病,不同的起始編號,國際化的低支援,JDK官方也認識到這個問題,後台提出使用Calendar類進行日期操作,日期的格式化交給DateFormat,雖然我們已經不再使用Date類中的大多數方法,但是還有一部分保留的內容指的我們一談。

2、構造器

  Date類之前有6大構造器,其中四個已經標註棄用,我們我不再看他,我們重點看另外兩個:

 1     /** 2      * Allocates a <code>Date</code> object and initializes it so that 3      * it represents the time at which it was allocated, measured to the 4      * nearest millisecond. 5      * 6      * @see     java.lang.System#currentTimeMillis() 7      */ 8     public Date() { 9         this(System.currentTimeMillis());10     }11 12     /**13      * Allocates a <code>Date</code> object and initializes it to14      * represent the specified number of milliseconds since the15      * standard base time known as "the epoch", namely January 1,16      * 1970, 00:00:00 GMT.17      *18      * @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.19      * @see     java.lang.System#currentTimeMillis()20      */21     public Date(long date) {22         fastTime = date;23     }

  第一個構造器是無參構造器,通過調用System的currentTimeMillis()方法來擷取目前時間戳,這個時間戳記是從1970年到目前時間的毫秒級資料,第二個構造器,可以將一個毫秒級的資料定義為Date格式的日期。

3、常用方法

  Date中定義了諸多的日期操作方法,但是大多數都已棄用,只剩餘為數不多的幾個方法:

  /**     * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT     * represented by this <tt>Date</tt> object.     *     * @return  the number of milliseconds since January 1, 1970, 00:00:00 GMT     *          represented by this date.     */    public long getTime() {        return getTimeImpl();    }    /**     * Sets this <code>Date</code> object to represent a point in time that is     * <code>time</code> milliseconds after January 1, 1970 00:00:00 GMT.     *     * @param   time   the number of milliseconds.     */        public void setTime(long time) {        fastTime = time;        cdate = null;    }    /**     * Tests if this date is before the specified date.     *     * @param   when   a date.     * @return  <code>true</code> if and only if the instant of time     *            represented by this <tt>Date</tt> object is strictly     *            earlier than the instant represented by <tt>when</tt>;     *          <code>false</code> otherwise.     * @exception NullPointerException if <code>when</code> is null.     */    public boolean before(Date when) {        return getMillisOf(this) < getMillisOf(when);    }    /**     * Tests if this date is after the specified date.     *     * @param   when   a date.     * @return  <code>true</code> if and only if the instant represented     *          by this <tt>Date</tt> object is strictly later than the     *          instant represented by <tt>when</tt>;     *          <code>false</code> otherwise.     * @exception NullPointerException if <code>when</code> is null.     */    public boolean after(Date when) {        return getMillisOf(this) > getMillisOf(when);    }

  上面顯示的四個方法是Date類中現在還在使用的幾個常用方法:

    long getTime()方法:返回從1970年00:00:00到Date對象所代表時間的毫秒級資料

    void setTime(long time)方法:設定一個Date對象用來代表從1970年00:00:00開始的一段毫秒級資料後所代表的時間點

    boolean before(Date when)方法:判斷Date對象所代表的時間點是否在when所代表的時間點之前

    boolean after(Date when)方法:判斷Date對象所代表的時間點是否在when所代表的時間點之後

4、其他

  Date類實現了java.io.Serializable介面,可以執行序列化與還原序列化操作,在Date類中定義了writeObject(ObjectOutputStream s)方法和readObject(ObjectInputStream s)方法,分別用於在Date對象進行序列化和還原序列化操作時將對象所代表的時間戳記(long型資料)進行儲存與擷取,因為fastTime欄位採用transient修飾,其內容會被序列化機制過濾掉,而這個欄位內儲存的是Date對象所代表時間的時間戳記(long型)

有關內容詳情見《Java常用API解析——序列化API(一)》

 1     private transient long fastTime; 2  3     /** 4      * Save the state of this object to a stream (i.e., serialize it). 5      * 6      * @serialData The value returned by <code>getTime()</code> 7      *             is emitted (long).  This represents the offset from 8      *             January 1, 1970, 00:00:00 GMT in milliseconds. 9      */10     private void writeObject(ObjectOutputStream s)11          throws IOException12     {13         s.writeLong(getTimeImpl());14     }15 16     /**17      * Reconstitute this object from a stream (i.e., deserialize it).18      */19     private void readObject(ObjectInputStream s)20          throws IOException, ClassNotFoundException21     {22         fastTime = s.readLong();23     }

5、執行個體解析:

 1     public static void main(String[] args) { 2         Date now = new Date();//擷取目前時間 3         Date when = new Date(10201020097865L);//根據時間戳記定義指定時間點 4         boolean b1 = now.after(when); 5         boolean b2 = now.before(when); 6         Long d1 = now.getTime(); 7         Long d2 = when.getTime(); 8          9         System.out.println("now值為:"+now);10         System.out.println("when值為:"+when);11         System.out.println("b1值為:"+b1);12         System.out.println("b2值為:"+b2);13         System.out.println("d1值為:"+d1);14         System.out.println("d2值為:"+d2);15         16     }

結果為:

now值為:Thu Jul 06 13:39:12 CST 2017when值為:Tue Apr 04 16:41:37 CST 2293b1值為:falseb2值為:trued1值為:1499319552116d2值為:10201020097865

 6、總結

  Date類現在並不推薦使用,Java推薦了Calendar和DateFormat,甚至SimpleDateFormat來替代它,Date中僅剩的幾個方法仍然還很實用,尤其是before與after方法,可以很方便的判斷兩個時間點的先後,當然判斷的條件是將你的時間轉換成Date格式,使用Date剩餘的兩個構造器實現即可,當然也可以使用推薦的SimpleDateFormat方法進行簡單的格式化日期格式字串的方式得到Date格式的時間點,這些會在稍後瞭解到!

    

java基礎系列--Date類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.