如何检查两个日期是否在一个周期(间隔)之间? [英] How can I check that a two dates are between a period(interval)?
问题描述
我的输入是开始日期和结束日期。我想检查它是在12月1日到3月31日之间(年份可以更改,只有日期或此日期之外的日期)。
My input is a beginning date and an end date. And I want to check that it is between December 1 and March 31. (The year can change, and there will be only dates in, or dates outside this period).
到目前为止,我没有找到任何解决方案与Joda时间。有人可以给我一个起点(不是代码,只是逻辑)吗?
我没有检查代码,但它是非常丑陋的,我想找到一个算法解决方案
So far I didn't found any solution with Joda-time. Can somebody give me a starting point how to do(not code, just the logic)? I didn't checked the code yet, but it is VERY Ugly, and I want to find an algorithmic solution
public static boolean isInWinter(Contract contract) {
logger.info("begin isInWinter:");
DateTime beginDate = contract.getBeginningDate();
DateTime endDate = contract.getEndDate();
/*
* If the year is different (etc 2012 dec,2013 marc) check that the
* beginning month is december, and the end month is jan,feb,marc
*/
if (endDate.getYear() - beginDate.getYear() == 1) {
if ((beginDate.getMonthOfYear() == 12)
&& ((endDate.getMonthOfYear() == 1
|| endDate.getMonthOfYear() == 2 || endDate
.getMonthOfYear() == 3))) {
logger.info("return true different year");
return true;
}
/*
* Same year can be if begin and end date is december or begin and
* and date is jan,febr,marc TODO REMOVE Auto formatter
*/
} else if (endDate.getYear() - beginDate.getYear() == 0) {
if ((beginDate.getMonthOfYear() == 12 && endDate.getMonthOfYear() == 12)
|| ((beginDate.getMonthOfYear() == 1
|| beginDate.getMonthOfYear() == 2 || beginDate
.getMonthOfYear() == 3) && (endDate
.getMonthOfYear() == 1
|| endDate.getMonthOfYear() == 2 || endDate
.getMonthOfYear() == 3))) {
logger.info("return true same year");
return true;
}
}
logger.info("return false");
return false;
}
推荐答案
Joda-Time
Joda-Time 及其间隔类与重叠
方法就是你所需要的。非常容易。
Joda-Time
Joda-Time and its Interval class with the overlap
method is just what you need. Very easy.
与java.util.Date不同,DateTime真正知道其分配的时区。一般比指定要比依赖默认。请注意,如果您跟踪日期+时间(DateTime而不是LocalDate),则时区至关重要。 日开始和结束的定义取决于时区。跨越时区工作的企业可能会选择使用UTC进行此目的。
Unlike java.util.Date, a DateTime truly knows its assigned time zone. Generally better to specify than rely on default. Note that if you are tracking date+time (a DateTime rather than a LocalDate), then time zone is critical. The definition of a "day" beginning and ending depends on time zone. Businesses that work across time zones may choose to use UTC for this purpose.
除了Interval之外,Joda-Time还提供了一段时间工作的期间和期限课程。
FYI, besides Interval, Joda-Time also offers the Period and Duration classes for working with a span of time.
当使用日期时间值,但关注天时,请将时间部分调整到该特定时区的一天中的第一时刻。在Joda-Time中,只需调用 withTimeAtStartOfDay
方法即可。
When working with date-time values but focusing on a "day", then adjust the time portion to the first moment of the day for that particular time zone. In Joda-Time, simply call the withTimeAtStartOfDay
method.
避免与午夜相关的类和方法。他们不再由Joda-Time团队推荐。
Avoid the "midnight"-related classes and methods. They are no longer recommended by the Joda-Time team.
Joda-Time使用比较时间跨度的半开方法。包容性和结局的开始是独家的。这意味着,从12月1日到3月31日,我们将12月1日当天的第一时间定为到
的第一时刻4月1日当天
。如果你想这个,并且搜索StackOverflow在joda间隔上的其他帖子,你会看到这种方法的智慧。
Joda-Time uses the "Half-Open" approach when comparing spans of time. The beginning in inclusive and the ending is exclusive. That means since you want from December 1 to March 31 that we define an Interval of the first moment of the day of December 1
to the first moment of the day of April 1
. If you ponder this, and search StackOverflow for other postings on "joda interval", you'll come to see the wisdom of this approach.
Interval类提供方便的比较方法: 包含
, abuts
,重叠
和 gap
。确保阅读文档以了解详细信息。
The Interval class offers handy comparison methods: contains
, abuts
, overlap
, and gap
. Be sure to read the doc to understand exact details.
Java 8带来了新的java时间包,由Joda-Time定义,由JSR 310定义.Joda-Time继续在Java 8中工作,但如果您刚开始使用日期时间,则可能需要学习java.time而不是Joda-Time。 / p>
示例代码
Java 8 brings the new java.time package, inspired by Joda-Time, defined by JSR 310. Joda-Time continues to work in Java 8, but you may want to learn java.time rather than Joda-Time if you are new to date-time work.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Budapest" ); // Or, perhaps DateTimeZone.UTC
// Inputs
DateTime contractStart = new DateTime( 2014, 1, 2, 0, 0, 0, timeZone );
DateTime contractStop = new DateTime( 2014, 5, 6, 0, 0, 0, timeZone );
Interval contractInterval = new Interval( contractStart, contractStop );
// Target to test
DateTime targetStart = new DateTime( 2013, DateTimeConstants.DECEMBER, 1, 0, 0, 0, timeZone );
DateTime targetStop = targetStart.plusMonths( 4 );
Interval targetInterval = new Interval( targetStart, targetStop );
boolean targetContainsContract = targetInterval.contains( contractInterval );
boolean targetOverlapsContract = ( targetInterval.overlap( contractInterval ) != null );
转储到控制台...
System.out.println( "contractInterval: " + contractInterval );
System.out.println( "targetInterval: " + targetInterval );
System.out.println( "targetContainsContract: " + targetContainsContract );
System.out.println( "targetOverlapsContract: " + targetOverlapsContract );
运行时
contractInterval: 2014-01-02T00:00:00.000+01:00/2014-05-06T00:00:00.000+02:00
targetInterval: 2013-12-01T00:00:00.000+01:00/2014-04-01T00:00:00.000+02:00
targetContainsContract: false
targetOverlapsContract: true
这篇关于如何检查两个日期是否在一个周期(间隔)之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!