Java中Date类是一个表示日期和时间的类,它是继承自Object类的一个类。在Java中,所有日期和时间都是通过Date类来进行处理的。下面我们来解析一下Date类的源码。
public class Date implements java.io.Serializable, Cloneable, Comparable<Date> {
/**
* The Class Constructor.
*/
public Date() {
this(System.currentTimeMillis());
}
/**
* Constructs a Date object that represents 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 number of milliseconds since January 1, 1970, 00:00:00 GMT.
*/
public Date(long date) {
fastTime = date;
}
/**
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
* represented by this Date object.
*
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
*/
public long getTime() {
return fastTime;
}
/**
* Sets this Date object to represent a point in time that is the
* specified number of milliseconds after January 1, 1970, 00:00:00 GMT.
*
* @param time the number of milliseconds.
*/
public void setTime(long time) {
fastTime = time;
}
/**
* Returns a string representation of this date and time.
*
* @return a string representation of this date and time.
*/
public String toString() {
// ...
}
/**
* Tests if this date is before the specified date.
*
* @param when a date.
* @return true if and only if the instant of time represented by this
* Date object is strictly earlier than the instant represented by when;
* false otherwise.
*/
public boolean before(Date when) {
return fastTime < when.fastTime;
}
/**
* Tests if this date is after the specified date.
*
* @param when a date.
* @return true if and only if the instant represented by this
* Date object is strictly later than the instant represented by when;
* false otherwise.
*/
public boolean after(Date when) {
return fastTime > when.fastTime;
}
/**
* Compares two dates for equality.
*
* @param obj the object to compare with.
* @return true if and only if the argument is not null and is a Date object
* that represents the same point in time, to the millisecond, as this
* object; false otherwise.
*/
public boolean equals(Object obj) {
return obj instanceof Date && fastTime == ((Date) obj).fastTime;
}
/**
* Returns the hash code value for this object.
*
* @return a hash code value for this object.
*/
public int hashCode() {
return (int) fastTime;
}
/**
* Clones this object.
*
* @return a clone of this object.
*/
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
/**
* Compares two Dates for ordering.
*
* @param anotherDate the Date to be compared.
* @return the value 0 if the argument Date is equal to this Date; a value less than
* 0 if this Date is before the Date argument; and a value greater than 0
* if this Date is after the Date argument.
*/
public int compareTo(Date anotherDate) {
long thisTime = getMillisOf(this);
long anotherTime = getMillisOf(anotherDate);
return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}
// ... Other methods
/**
* The number of milliseconds per day.
* @since 1.6
*/
static final public long ONE_DAY = 24 * 60 * 60 * 1000;
/**
* The number of milliseconds per hour.
* @since 1.6
*/
static final public long ONE_HOUR = 60 * 60 * 1000;
/**
* The number of milliseconds per minute.
* @since 1.6
*/
static final public long ONE_MINUTE = 60 * 1000;
/**
* The number of milliseconds per second.
* @since 1.6
*/
static final public long ONE_SECOND = 1000;
/**
* Determines whether this date object is within the specified
* number of milliseconds of the system time.
*
* @param delta the allowed difference, in milliseconds.
* @return true if the difference between the system time and this
* date object is less than or equal to delta.
*/
public boolean withinDelta(long delta) {
return Math.abs(System.currentTimeMillis() - fastTime) <= delta;
}
private transient long fastTime;
// ... Other constants and fields
}
Date类中含有一个long类型的fastTime字段,它存储了日期和时间的毫秒数,另外还有一些常量以及一些和日期和时间操作相关的方法,下面我们分别解析一下。
构造方法和getTime()/setTime()方法
Date类中提供了两个构造方法,一个是无参构造方法,该方法会使用System.currentTimeMillis()方法获取当前时间的毫秒值,然后将该值赋值给fastTime字段;另一个是有参构造方法,该方法需要传入一个表示日期和时间的毫秒数,然后再将该值赋值给fastTime字段。
Date类中还提供了两个方法getTime()和setTime(long time),其中getTime()方法可以获取该Date对象所表示日期和时间的毫秒数,而setTime(long time)方法则可以将该Date对象所表示日期和时间的毫秒数设置为指定的毫秒数。
toString()方法
Date类中的toString()方法可以返回一个表示该Date对象所表示日期和时间的字符串形式,例如:
Sat Nov 21 22:43:10 CST 2020
before()/after()/equals()方法
Date类中提供了三个比较日期和时间的方法,分别是before(Date when)、after(Date when)和equals(Object obj)方法。其中,before(Date when)方法用于判断该Date对象所表示的日期和时间是否早于when对象所表示的日期和时间;after(Date when)方法用于判断该Date对象所表示的日期和时间是否晚于when对象所表示的日期和时间;equals(Object obj)方法用于判断该Date对象所表示的日期和时间是否和obj对象所表示的日期和时间一致。
compareTo()方法
Date类实现了Comparable<Date>接口,该接口中有一个compareTo(Date anotherDate)方法。这个方法用于比较两个日期和时间的先后顺序,如果该Date对象所表示的日期和时间早于anotherDate对象所表示的日期和时间,则返回-1;如果它们相等,则返回0;如果它们晚于anotherDate对象所表示的日期和时间,则返回1。
withinDelta()方法
Date类中的withinDelta(long delta)方法可以判断该Date对象所表示的日期和时间是否在当前系统时间的delta毫秒之内,如果在则返回true,否则返回false。
常量
Date类中还定义了一些和日期和时间操作相关的常量,包括:ONE_DAY(一天的毫秒数)、ONE_HOUR(一小时的毫秒数)、ONE_MINUTE(一分钟的毫秒数)以及ONE_SECOND(一秒的毫秒数)。这些常量可以用于一些日期和时间的计算中。