java 如何只比较Java日期中的一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6398843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:44:26  来源:igfitidea点击:

How to compare just the day in a java date?

javadate

提问by Toran Billups

I'm trying to do a simple date comparison between yesterday and today

我正在尝试在昨天和今天之间做一个简单的日期比较

if (yesterday.before(today)) {
 ...
}

The issue is that with the before method I will eval to true even if it's just a few seconds difference. How might I compare just the day (because I only want to eval this to true if it was the previous day (minutes/seconds should be ignored)

问题是,使用 before 方法,即使只有几秒钟的差异,我也会评估为 true。我如何只比较当天(因为如果是前一天,我只想将其评估为真(应忽略分/秒)

Thank you in advance

先感谢您

回答by Praveen Lobo

using DateUtils-

使用DateUtils-

if (!DateUtils.isSameDay(yesterday, today) && (yesterday.before(today)) {
   //...
}

EDIT: it can be done without DateUtils as well. See thisthread.

编辑:它也可以在没有 DateUtils 的情况下完成。看到这个线程。

回答by s106mo

If you don't want to use a 3rd party library implement a method like this:

如果您不想使用第 3 方库,请实现如下方法:

public boolean before(Calendar yesterday, Calendar today) {
    if(yesterday == today) return false;
    if(yesterday == null || today == null) return false;
    return  yesterday.get(Calendar.YEAR) < today.get(Calendar.YEAR) ? true : 
        yesterday.get(Calendar.YEAR) == today.get(Calendar.YEAR) && yesterday.get(Calendar.DAY_OF_YEAR) < today.get(Calendar.DAY_OF_YEAR);
}

回答by Don Roby

If you're up to adding a library that handles dates better than the standard Java libraries, you might want to look at Joda.

如果您要添加一个比标准 Java 库更好地处理日期的库,您可能需要查看Joda

Using Joda, you can compute difference between days by:

使用 Joda,您可以通过以下方式计算天之间的差异:

  Days d = Days.daysBetween(startDate, endDate);
  int days = d.getDays();

where startDateand endDateare the Joda version of dates, DateTime(actually a superclass of that).

其中startDateendDate是 Joda 版本的日期,DateTime(实际上是它的超类)。

Converting Java Dateobjects to Joda DateTimeobjects can be done by a constructor call:

可以通过构造函数调用将 JavaDate对象转换为 JodaDateTime对象:

DateTime dateTime = new DateTime(javaDate);

Adding this library may be overkill for this specific problem, but if you deal with date and time manipulation a lot, the library is definitely worth it.

添加这个库对于这个特定的问题可能有点矫枉过正,但如果你经常处理日期和时间操作,那么这个库绝对值得。

回答by pdresselhaus

If you want to stick to Dateyou could temporarily lower your current date by one day. If before()still leads to the same result you have a timespan of at least one day.

如果你想坚持,Date你可以暂时将当前日期减少一天。如果before()仍然导致相同的结果,则您的时间跨度至少为一天。

final static long DAY_MILLIS = 86400000;

Date d1 = new Date(2011, 06, 18);
Date d2 = new Date(2011, 06, 16);

Date temp = new Date(d1.getTime() - DAY_MILLIS);

if (temp.before(d2)) 
  // Do stuff
}

Please note I used a deprecated constructor but it should do the job.

请注意,我使用了一个已弃用的构造函数,但它应该可以完成这项工作。