关于Date
1. java.util.Date
java.util.Date类中有一个 fastTime属性,此属性保存时间的long类型值,Date类型在java中是以long类型保存的,与1970.01.01
的时间差。
2.获取当前时间
Date now = new Date( ); //获得Date类型当前时间
long now = new Date( ).getTime(); //获得long类型当前时间
jdk中相应构造函数如下,可看出在构造Date时是将系统当前时间的毫秒表示赋值给fastTime属性。
/**
* 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;
}
Date类的getTime( )方法则是返回faseTime属性。
/**
* 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();
}
private final long getTimeImpl() {
if (cdate != null && !cdate.isNormalized()) {
normalize();
}
return fastTime;
}