比较时间是否到期

博客讨论了如何在Java中正确地比较日期以判断是否过期。解决方案包括将日期转换为毫秒数进行比较,使用Calendar类的方法,以及Date对象的compareTo()方法。建议避免直接使用'><'符号进行比较,而应利用提供的API函数确保准确判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何判断日期是否过期??
例如:日期A 日期B 日期C (格式全为日期型)

如果 B 小于A 输出:还没开始
如果 B 大于C 输出:已经结束

否则 正在进行中。。。。

请请教这个怎么实现。。。。我用“> 、 <”判断不管用!

------解决方案--------------------
分别获得这三个日期的毫秒数,然后进行比较!

DATE API 中有这个函数 我忘了
呵呵!
------解决方案--------------------
Calendar now = Calendar.getInstance();
Calendar auditTime = Calendar.getInstance();

auditTime.clear();
auditTime.setTime(vktask.getEndDate());//vktask.getEndDate()取出的是java.util.Date型

if (auditTime.before(now)) {
}else{
}
------解决方案--------------------
可以使用 Date 类的 compareTo() 方法来比较。

data1.compareTo(data2); 会返回一个整数值,小于0,这个时间在前;大于0,这个时间在后;等于0,这两个时间相等。
------解决方案--------------------
对,直接把2007-06-06 15:15:00 这种格式的时间转化成毫秒数,然后直接比较!
------解决方案--------------------
before
public boolean before(Date when)
Tests if this date is before the specified date.

Parameters:
when - a date.
Returns:
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.


after
public boolean after(Date when)
Tests if this date is after the specified date.

Parameters:
when - a date.
Returns:
true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.


转自:
http://www.myexceptions.net/java-web/39099.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值