黑马程序员技术交流社区

标题: 如何判断第二个日期比第一个日期大? [打印本页]

作者: 纳海百川    时间: 2015-1-26 21:32
标题: 如何判断第二个日期比第一个日期大?
录入DateTime格式的日期 怎么比较二个的大小啊?
作者: play1369    时间: 2015-1-28 16:36
用日期类的compareTo方法,会比较日期早晚
作者: 程豆浆    时间: 2015-1-28 18:34
同学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小。明白了吗?
作者: Lo_Twilight    时间: 2015-1-28 21:45
俩循环啊~
作者: 流星划过的黎明    时间: 2015-2-1 23:30
compareTo
public int compareTo(Date anotherDate)比较两个日期的顺序。

指定者:
接口 Comparable<Date> 中的 compareTo
参数:
anotherDate - 要比较的 Date。
返回:
如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2