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 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 格式的日期。
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 voidsetTime(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) {
- returngetMillisOf(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) {
- returngetMillisOf(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 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 {
- 22fastTime = s.readLong();
- 23}
5、实例解析:
- 1 public static void main(String[] args) {
- 2Date now =newDate();//获取当前时间
- 3Date when =newDate(10201020097865L);//根据时间戳定义指定时间点
- 4 booleanb1 = now.after(when);
- 5 booleanb2 = now.before(when);
- 6Long d1 = now.getTime();
- 7Long d2 = when.getTime();
- 8
- 9System.out.println("now值为:"+now);
- 10System.out.println("when值为:"+when);
- 11System.out.println("b1值为:"+b1);
- 12System.out.println("b2值为:"+b2);
- 13System.out.println("d1值为:"+d1);
- 14System.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
6、总结
Date 类现在并不推荐使用,Java 推荐了 Calendar 和 DateFormat,甚至 SimpleDateFormat 来替代它,Date 中仅剩的几个方法仍然还很实用,尤其是 before 与 after 方法,可以很方便的判断两个时间点的先后,当然判断的条件是将你的时间转换成 Date 格式,使用 Date 剩余的两个构造器实现即可,当然也可以使用推荐的 SimpleDateFormat 方法进行简单的格式化日期格式字符串的方式得到 Date 格式的时间点,这些会在稍后了解到!
来源: http://www.cnblogs.com/V1haoge/p/7126930.html