Approach-1 : Using Date.before(), Date.after() and Date.equals()
if(date1.after(date2)){
System.out.println("Date1 is after Date2");
}
if(date1.before(date2)){
System.out.println("Date1 is before Date2");
}
if(date1.equals(date2)){
System.out.println("Date1 is equal Date2");
}
Approach-2 : Date.compareTo()
if(date1.compareTo(date2)>0){
System.out.println("Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
System.out.println("Date1 is before Date2");
}else{
System.out.println("Date1 is equal to Date2");
}
Approach-3 : Calender.before(), Calender.after() and Calender.equals()
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
if(cal1.after(cal2)){
System.out.println("Date1 is after Date2");
}
if(cal1.before(cal2)){
System.out.println("Date1 is before Date2");
}
if(cal1.equals(cal2)){
System.out.println("Date1 is equal Date2");
}
116
tl;博士
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
Boolean isBetween =
( ! today.isBefore( localDate1 ) ) // “not-before” is short for “is-equal-to or later-than”.
&&
today.isBefore( localDate3 ) ;
Date today = new Date();
Date myDate = new Date(today.getYear(),today.getMonth()-1,today.getDay());
System.out.println("My Date is"+myDate);
System.out.println("Today Date is"+today);
if (today.compareTo(myDate)<0)
System.out.println("Today Date is Lesser than my Date");
else if (today.compareTo(myDate)>0)
System.out.println("Today Date is Greater than my date");
else
System.out.println("Both Dates are equal");
if (date1.getTime() <= date.getTime() && date.getTime() <= date2.getTime()) {
/*
* date is between date1 and date2 (both inclusive)
*/
}
/*
* when date1 = 2015-01-01 and date2 = 2015-01-10 then
* returns true for:
* 2015-01-01
* 2015-01-01 00:00:01
* 2015-01-02
* 2015-01-10
* returns false for:
* 2014-12-31 23:59:59
* 2015-01-10 00:00:01
*
* if one or both dates are exclusive then change <= to <
*/
11 回答
使用getTime()获取日期的数值,然后使用返回的值进行比较 .
试试这个
Date有before和after方法,可以compared to each other如下:
对于包容性比较:
你也可以给Joda-Time一个去,但请注意:
后端端口可用于Java 6和7以及Android .
使用compareTo:
date1.compareTo(date2);
以下是比较日期的最常用方法 . 但我更喜欢第一个
Approach-1 : Using Date.before(), Date.after() and Date.equals()
Approach-2 : Date.compareTo()
Approach-3 : Calender.before(), Calender.after() and Calender.equals()
tl;博士
或者,更好的是,如果您将ThreeTen-Extra库添加到项目中 .
半开放方式,开始是包容性的,而结束是排他性的 .
格式选择不当
顺便说一句,对于日期或日期时间值的文本表示,这是一种错误的格式选择 . 只要有可能,坚持使用标准的ISO 8601格式 . ISO 8601格式是明确的,可以在人类文化中理解,并且易于通过机器解析 .
对于仅日期值,标准格式为YYYY-MM-DD . 请注意,这种格式在按字母顺序排序时具有按时间顺序排列的优点 .
LocalDate
LocalDate类表示没有时间且没有时区的仅日期值 .
时区对于确定日期至关重要 . 对于任何给定的时刻,日期在全球范围内因地区而异 . 例如,在Paris France午夜后几分钟是新的一天,而在Montréal Québec仍然是“昨天” .
DateTimeFormatter
由于您的输入字符串是非标准格式,我们必须定义要匹配的格式设置模式 .
用它来解析输入字符串 .
在日期时间工作中,通常最好通过半开放方法定义时间 Span ,其中开头是包含在内的,而结尾是独占的 . 因此,我们想知道今天是否与开始时相同或晚于停止之前 . 一种简单的说法“与开始相同或晚于”的方式是“不在开始之前” .
请参阅the Answer by gstackoverflow,其中显示了您可以调用的比较方法列表 .
关于java.time
java.time框架内置于Java 8及更高版本中 . 这些类取代了麻烦的旧legacy日期时间类,例如java.util.Date,Calendar和SimpleDateFormat .
现在位于maintenance mode的Joda-Time项目建议迁移到java.time类 .
要了解更多信息,请参阅Oracle Tutorial . 并搜索Stack Overflow以获取许多示例和解释 . 规格是JSR 310 .
从哪里获取java.time类?
Java SE 8和SE 9以及之后
内置 .
具有捆绑实现的标准Java API的一部分 .
Java 9增加了一些小功能和修复 .
Java SE 6和SE 7
大部分java.time功能都在ThreeTen-Backport中反向移植到Java 6&7 .
Android
ThreeTenABP项目专门针对Android调整ThreeTen-Backport(如上所述) .
见How to use ThreeTenABP… .
ThreeTen-Extra项目使用其他类扩展java.time . 该项目是未来可能添加到java.time的试验场 . 您可以在这里找到一些有用的类,例如Interval,YearWeek,YearQuarter和more .
更新:下面的“Joda-Time”部分保留为历史记录 . 现在位于maintenance mode的Joda-Time项目建议迁移到java.time类 .
Joda-Time
关于捆绑的java.util.Date和java.util.Calendar类,其他答案是正确的 . 但这些课程非常麻烦 . 所以这是使用Joda-Time 2.3库的一些示例代码 .
如果你真的想要一个没有任何时间部分和没有时区的日期,那么在Joda-Time中使用LocalDate类 . 该类提供了比较方法,包括
compareTo
(与Java Comparators一起使用),_isBefore
,isAfter
和isEqual
.输入...
定义描述输入字符串的格式化程序......
使用formatter进行解析将字符串转换为LocalDate对象......
转储到控制台......
跑的时候......
那么看看第二个是否在另外两个之间(完全,意思是不等于任何一个 endpoints )......
与时间 Span 合作
如果您正在使用时间 Span ,我建议您在Joda-Time中探索类:Duration,Interval和Period . 诸如
overlap
和contains
之类的方法使比较变得容易 .对于文本表示,请查看ISO 8601标准:
duration
格式:PnYnMnDTnHnMnS
示例:P3Y6M4DT12H30M5S
(表示“三年,六年,四天,十二小时,三十五,五秒”)
interval
格式:开始/结束
示例:2007-03-01T13:00:00Z / 2008-05-11T15:30:00Z
Joda-Time类可以使用这两种格式的字符串,包括输入(解析)和输出(生成字符串) .
Joda-Time使用 Half-Open 方法进行比较,其中 Span 的开始是包含的,而结尾是独占的 . 这种方法对于处理时间 Span 是明智的 . 搜索StackOverflow以获取更多信息 .
比较两个日期:
Java 8及更高版本的更新
isAfter()
isBefore()
isEqual()
compareTo()
这些方法存在于LocalDate,LocalTime和LocalDateTime类中 .
这些类内置于Java 8及更高版本中 . 大部分java.time功能在ThreeTen-Backport中反向移植到Java 6和7,并进一步适应ThreeTenABP中的Android(参见How to use…) .
你可以使用Date.getTime():
这意味着您可以像数字一样比较它们:
此代码确定今天是基于KOREA语言环境的某个持续时间
这个方法对我有用: