A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

录入DateTime格式的日期 怎么比较二个的大小啊?

评分

参与人数 1黑马币 +6 收起 理由
秦文强 + 6

查看全部评分

4 个回复

倒序浏览
用日期类的compareTo方法,会比较日期早晚
回复 使用道具 举报
同学Java API中java.util.Date类中有一个compareTo的方法,给你拷贝到这里了:
public int compareTo(Date anotherDate)

Compares two Dates for ordering.
Specified by:compareTo in interface Comparable<Date>Parameters:anotherDate - the Date to be compared.


Returns: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.Throws:NullPointerException - if anotherDate is null.Since:1.2

主要看的是最后一段,我给你翻译一下:
返回值:如果返回值是0 的话说明这两个日期相等,如果返回值小于零的话就说明这个日期在anotherDate之前,也就是比anotherDate大,如果大于0说明这个日期在anotherDate之后,也就说这个日期比anotherDate小。明白了吗?
回复 使用道具 举报
俩循环啊~
回复 使用道具 举报
compareTo
public int compareTo(Date anotherDate)比较两个日期的顺序。

指定者:
接口 Comparable<Date> 中的 compareTo
参数:
anotherDate - 要比较的 Date。
返回:
如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马