原創做品,能夠轉載,可是請標註出處地址:http://www.cnblogs.com/V1haoge/p/7126930.htmlhtml
一、Date類概述java
Date類是從JDK1.1就開始存在的老類,其提供了針對日期進行操做的諸多方法,但其卻一直飽受詬病,不一樣的起始編號,國際化的低支持,JDK官方也認識到這個問題,後臺提出使用Calendar類進行日期操做,日期的格式化交給DateFormat,雖然咱們已經再也不使用Date類中的大多數方法,可是還有一部分保留的內容指的咱們一談。post
二、構造器this
Date類以前有6大構造器,其中四個已經標註棄用,咱們我再也不看他,咱們重點看另外兩個:url
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 to 14 * represent the specified number of milliseconds since the 15 * 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格式的日期。spa
三、經常使用方法rest
Date中定義了諸多的日期操做方法,可是大多數都已棄用,只剩餘爲數很少的幾個方法:code
/** * 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類中如今還在使用的幾個經常使用方法:orm
long getTime()方法:返回從1970年00:00:00到Date對象所表明時間的毫秒級數據htm
void setTime(long time)方法:設置一個Date對象用來表明從1970年00:00:00開始的一段毫秒級數據後所表明的時間點
boolean before(Date when)方法:判斷Date對象所表明的時間點是否在when所表明的時間點以前
boolean after(Date when)方法:判斷Date對象所表明的時間點是否在when所表明的時間點以後
四、其餘
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 IOException 12 { 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, ClassNotFoundException 21 { 22 fastTime = s.readLong(); 23 }
五、實例解析:
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 2017 when值爲:Tue Apr 04 16:41:37 CST 2293 b1值爲:false b2值爲:true d1值爲:1499319552116 d2值爲:10201020097865
六、總結
Date類如今並不推薦使用,Java推薦了Calendar和DateFormat,甚至SimpleDateFormat來替代它,Date中僅剩的幾個方法仍然還很實用,尤爲是before與after方法,能夠很方便的判斷兩個時間點的前後,固然判斷的條件是將你的時間轉換成Date格式,使用Date剩餘的兩個構造器實現便可,固然也可使用推薦的SimpleDateFormat方法進行簡單的格式化日期格式字符串的方式獲得Date格式的時間點,這些會在稍後瞭解到!