使用日历比较Java 7中的日期 [英] Comparing Dates in Java 7 using Calendar

查看:160
本文介绍了使用日历比较Java 7中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一项条约的开始日期与其结束日期进行比较,以检查其持续时间是否超过6个月。如果期间不包括2月,那么一切都很好,但是如果我将1月1日与6月30日进行比较,则会抛出异常。为了比较两个期间,我将6个月添加到开始日期,并将结果与​​结束日期进行比较,如下所示:

I am comparing the start Date of a treatement to its End to check if it lasts more than 6 months. If the period doesn't include the month of February all is good but if I compare the 1st of January to the 30th of June, it throws my Exception. To compare both periods I add 6 months to the start Date and compare the result to the end Date like so:

Date start = new Date(2017,1,1);
Date end = new Date(2017,6,30);

Calendar startOfTreatment = new Calendar.getInstance();
startOfTreatment.setTime(start);

Calendar endOfTreatment = new Calendar.getInstance();
endOfTreatment.setTime(end);

startOfTreatment.add(Calendar.MONTH, 6);
if (startOfTreatment.compareTo(endOfTreatment) > 0) {
    throw new InfinishedTreatmentException(startOfTreatment,endOfTreatment);
}

我该如何解决?

推荐答案

Date 构造函数(例如您正在使用的构造函数: new Date(2017 ,1,1))不仅已弃用(因此应避免使用),但误导性,因为年份的索引时间是1900(因此2017年为3917),月份为零索引(值在零(一月)到十一(十二月)之间。因此,这与您的想法不同:

The Date constructors (like the one you're using: new Date(2017,1,1)) are not only deprecated (so you should avoid them) but also misleading, because years are indexed at 1900 (so 2017 becomes 3917) and months are zero-indexed (the values are in the range zero (January) to 11 (December)). So this doesn't behave as you think:

Date start = new Date(2017, 1, 1); // February 1st 3917
Date end = new Date(2017, 6, 30); // July 30th 3917

开始添加6个月时,它将变为 8月1日 st ,即结束之后。

要创建1月1日 st 和6月30日 ,您必须使用 month-1 而要设置为2017年,您必须使用117(2017年-1900年):

To create January 1st and June 30th you must use month - 1 and to have the year 2017 you must use 117 (2017 - 1900):

Date start = new Date(117, 0, 1); // January 1st 2017
Date end = new Date(117, 5, 30); // June 30th 2017

即使开始加6个月将是 7月1日 st ,该日期仍在结束之后(因此您的代码将引发异常)。

Even though, start plus 6 months will be July 1st, which still is after end (so your code will throw the exception).

旧类(日期日历 SimpleDateFormat )具有很多问题设计问题,它们已被新的API取代。

The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.

Java< = 7 中,您可以使用 ThreeTen Backport ,这是Java 8的新日期/时间类的绝佳反向端口。

In Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes.

此新API具有许多新的日期和时间类型,用于处理不同的情况。由于我们只处理日期(天/月/年),因此可以使用 org.threeten.bp.LocalDate

This new API has lots of new date and time types to deal with different situations. As we're dealing only with dates (day/month/year), we can use a org.threeten.bp.LocalDate:

LocalDate start = LocalDate.of(2017, 1, 1); // January 1st 2017
LocalDate end = LocalDate.of(2017, 6, 30); // June 30th 2017

// 6 months after start
LocalDate sixMonthsLater = start.plusMonths(6);
if (sixMonthsLater.isAfter(end)) {
    // throw exception
}

这篇关于使用日历比较Java 7中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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