【发布时间】:2018-03-19 07:27:01
【问题描述】:
如何使用比较运算符比较 Java 中的日期?
例子:
date1 是30-10-2017
date2 是 31-10-2017
date3 是30-10-2018
date2 在小于date3 应该为真时返回假。如果日期小于另一个日期,如何返回 true,否则返回 false?
这是我的代码:return (day < theDate.day) || (month < theDate.month) || (year < theDate.year);
以下是我目前的解决方案:
{
boolean check = false;
if(year < theDate.year)
{
return true;
}
if(year > theDate.year)
{
return false;
}
if(month < theDate.month)
{
check = true;
}
if(day < theDate.day)
{
check = true;
}
else
{
return false;
}
return check;
}
【问题讨论】:
-
我认为 java 不接受日期上的比较运算符。但您可以将
LocalDateAPI 与isBefore和isAfter方法一起使用。 -
你的日期类是什么类型的? (完全合格)是
java.util.Date,还是java.time.LocalDate或别的什么?
标签: java date comparison boolean-expression comparison-operators