本项目只是本人学习Java使用的,可能有错误,仅供参考
- IDEA 2021.1
- JDK1.8
该类大量方法已经被弃用,只有少量方法可以使用
源码
public Date(){
this(System.currentTimeMillis());
}功能
空参构造函数,调用自身的有参构造并传入System.currentTimeMillis()参数
源码
public Date(long date){
fastTime=date;
}分析
有参构造函数
源码
public long getTime(){
return getTimeImpl();
}功能
获取long类型的时间戳
分析
通过内部的getTimeImpl函数返回fastTime,最终 得到一个long类型的时间戳
源码
public void setTime(long time) {
fastTime = time;
cdate = null;
}功能
设置Date的fastTime属性
分析
将传进来的time给fastTime
源码
public boolean before(Date when) {
return getMillisOf(this) < getMillisOf(when);
}功能
判断指定when日期是否在Date日期之前
分析
通过获取两个日期的时间戳进行比较
源码
public boolean after(Date when) {
return getMillisOf(this) > getMillisOf(when);
}功能
判断指定when日期是否在Date日期之后
分析
通过获取两个日期的时间戳进行比较
源码
public boolean equals(Object obj) {
return obj instanceof Date && getTime() == ((Date) obj).getTime();
}功能
判断两个日期是否相等
分析
先判断参数是否是Date的实例,再强转使用==判断
源码
public int compareTo(Date anotherDate) {
long thisTime = getMillisOf(this);
long anotherTime = getMillisOf(anotherDate);
return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}功能
比较两个日期是否相等
分析
略
源码
public static Date from(Instant instant) {
try {
return new Date(instant.toEpochMilli());
} catch (ArithmeticException ex) {
throw new IllegalArgumentException(ex);
}
}功能
将Instant对象转换成Date对象
分析
略
源码
public Instant toInstant() {
return Instant.ofEpochMilli(getTime());
}功能
将Date转换成Instant对象
分析
略