检查日期是Java之间的两个日期之间 [英] Check a Date is between two dates in Java

查看:85
本文介绍了检查日期是Java之间的两个日期之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一件事我想知道的是如何计算从今天起10天的日期。



第二件事是检查一个Date是否在两个之间日期。
例如,假设我有一个应用程序显示在接下来的10天内需要做什么事情(计划者)。现在我如何查看我分配给某个活动的日期是否在今天和今天之前的10天之间?

解决方案

使用 java.util.Date java.util.Calendar 操纵和比较日期是一个痛苦的原因存在 JodaTime 。所有答案都没有涵盖时间。当日期不为零时,比较可能会失败。您是否想要包含排除的比较还不清楚。迄今为止发布的大多数答案表明,在5月20日至5月24日期间,比较(即5月24日是),而真正意义上说, (即5月20日至5月24日期间的5月24日 )。






< blockquote>

我想知道的一件事是如何计算从今天起10天的日期。


使用标准Java SE 6 API,您需要 java.util.Calendar

 code> Calendar plus10days = Calendar.getInstance(); 
plus10days.add(Calendar.DAY_OF_YEAR,10);

使用JodaTime,您将会这样做:

  DateTime plus10days = new DateTime()。plusDays(10); 







第二件事是检查一个日期是否在两个其他日期之间。例如,假设我有一个应用程序显示在接下来的10天内需要做什么事情(计划者)。现在如何查看我分配给某个活动的日期是从今天到今天的10天之间的日期?


现在是 Calendar 的可怕部分。让我们先准备一下:

  Calendar now = Calendar.getInstance(); 
日历plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR,10);
日历事件= Calendar.getInstance();
event.set(year,month - 1,day); //或setTime(date);

使用可靠地比较日历#before() Calendar#after(),我们需要先摆脱时间。想象一下,现在是2010年5月24日上午9点,事件的日期是2010年5月24日。当您想要包容性比较时,您希望在同一天使其返回true 。即(event.equals(now)|| event.after(now))或-shorter,但同样 - (!event.before(now ))应该返回true。但实际上没有这样做是因为在现在的时间的存在。您需要清除所有日历实例中的时间,如下所示:

  calendar.clear(Calendar.HOUR); 
calendar.clear(Calendar.HOUR_OF_DAY);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);

或者,您也可以仅在日/月/年比较。

  if(event.get(Calendar.YEAR)> = now.get(Calendar.YEAR)
&& event.get .MONTH)> = now.get(Calendar.MONTH)
&&&  event.get(Calendar.DAY_OF_MONTH)> = now.get(Calendar.DAY_OF_MONTH)
{
// event is equal or after today。
}

全部非常详细。



使用JodaTime,您可以使用 DateTime#toLocalDate() 仅获取日期部分:

  LocalDate now = new DateTime()。toLocalDate(); 
LocalDate plus10days = now.plusDays(10);
LocalDate event = new DateTime年份,月,日,0,0,0,0).toLocalDate();
if(!event.isBefore(now)&!event.isAfter(plus10days)){
/ /事件在现在到10天之间ys(含)。
}

是的,以上是真的所有要做。


One thing I want to know is how to calculate what date will it be 10 days from today.

Second thing is to check if one Date is between two other Dates. For example, let's say I have an app that shows what events I need to do in the next 10 days (planner). Now how can I see if the date I assigned to an event is between today and the date that is 10 days from today?

解决方案

Manipulating and comparing dates using java.util.Date and java.util.Calendar is pretty a pain, that's why JodaTime exist. None of the answers as far have covered the time in question. The comparisons may fail when the dates have a non-zero time. It's also unclear whether you want an inclusive or exclusive comparison. Most of the answers posted so far suggest exclusive comparision (i.e. May 24 is not between May 20 and May 24) while in real it would make more sense to make it inclusive (i.e. May 24 is between May 20 and May 24).


One thing I want to know is how to calculate what date will it be 10 days from today.

With the standard Java SE 6 API, you need java.util.Calendar for this.

Calendar plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR, 10);

With JodaTime you would do like this:

DateTime plus10days = new DateTime().plusDays(10);


Second thing is to check if one Date is between two other Dates. For example, let's say I have an app that shows what events I need to do in the next 10 days (planner). Now how can I see if the date I assigned to an event is between today and the date that is 10 days from today?

Now comes the terrible part with Calendar. Let's prepare first:

Calendar now = Calendar.getInstance();
Calendar plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR, 10);
Calendar event = Calendar.getInstance();
event.set(year, month - 1, day); // Or setTime(date);

To compare reliably using Calendar#before() and Calendar#after(), we need to get rid of the time first. Imagine it's currently 24 May 2010 at 9.00 AM and that the event's date is set to 24 May 2010 without time. When you want inclusive comparison, you would like to make it return true at the same day. I.e. both the (event.equals(now) || event.after(now)) or -shorter but equally- (!event.before(now)) should return true. But actually none does that due to the presence of the time in now. You need to clear the time in all calendar instances first like follows:

calendar.clear(Calendar.HOUR);
calendar.clear(Calendar.HOUR_OF_DAY);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);

Alternatively you can also compare on day/month/year only.

if (event.get(Calendar.YEAR) >= now.get(Calendar.YEAR)
    && event.get(Calendar.MONTH) >= now.get(Calendar.MONTH)
    && event.get(Calendar.DAY_OF_MONTH) >= now.get(Calendar.DAY_OF_MONTH)
{
    // event is equal or after today.
}

Very verbose all.

With JodaTime you can just use DateTime#toLocalDate() to get the date part only:

LocalDate now = new DateTime().toLocalDate();
LocalDate plus10days = now.plusDays(10);
LocalDate event = new DateTime(year, month, day, 0, 0, 0, 0).toLocalDate();
if (!event.isBefore(now) && !event.isAfter(plus10days)) {
    // Event is between now and 10 days (inclusive).
}

Yes, the above is really all you need to do.

这篇关于检查日期是Java之间的两个日期之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆