cdate在java中_JAVA中的日期比较问题

本文探讨了JAVA中Date对象的before和after方法的实现原理,这两个方法通过比较时间戳进行日期比较。同时指出某些Java实现可能直接调用getTime()。还详细解析了normalize方法和getTimeImpl()的作用,涉及日期的规范化和时间戳获取。
摘要由CSDN通过智能技术生成

public boolean before(Date when) {

return getMillisOf(this) < getMillisOf(when);

}

public boolean after(Date when) {

return getMillisOf(this) > getMillisOf(when);

}

public long getTime() {

return getTimeImpl();

}

private final long getTimeImpl() {

if (cdate != null && !cdate.isNormalized()) {

normalize();

}

return fastTime;

}

static final long getMillisOf(Date date) {

if (date.cdate == null || date.cdate.isNormalized()) {

return date.fastTime;

}

BaseCalendar.Date d = (BaseCalendar.Date) date.cdate.clone();

return gcal.getTime(d);

}

private final BaseCalendar.Date normalize() {

if (cdate == null) {

BaseCalendar cal = getCalendarSystem(fastTime);

cdate = (BaseCalendar.Date) cal.getCalendarDate(fastTime, TimeZone.getDefaultRef());

return cdate;

}

if (!cdate.isNormalized()) {

cdate = normalize(cdate);

}

TimeZone tz = TimeZone.getDefaultRef();

if (tz != cdate.getZone()) {

cdate.setZone(tz);

CalendarSystem cal = getCalendarSystem(cdate);

cal.getCalendarDate(fastTime, cdate);

}

return cdate;

}

看起来 before 和 after 会稍微快一点点。不过也有的 Java 实现里面 before 和 after 就是调用 getTime() 的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值