如何检查当前日期是否在java中两个重复发生的日期之间?

我正在尝试创建一个应用程序,如果今天是在学年,我就会陷入困境。 用户输入两个日期, 没有年份,每年重复一次 。 这些是学年的开始和结束日期。

我想检查当前日期是否在这两者之间,即使它重叠了两年。 因此,如果学校在11月开学,并在6月结束,如果今天是1月22日,它应该返回true。 但如果它的七月,它应该返回虚假。

我确实找到了这个问题: Php – 计算出它的学年 ,但它适用于学年,没有假期。

顺便说一下,如果有帮助,我有joda时间。

提前致谢。

//parse both enddate/startdate to same year if(enddate>startdate) enddate+=1 year; if(today>startdate and today 

好吧,我想了一下,找到了一些解决方案,但这似乎最简单。

 private boolean isInSchoolYear(DateTime now, DateTime schoolYearStart, DateTime schoolYearEnd){ int thisYear = now.getYear(); schoolYearStart = schoolYearStart.withYear(thisYear); schoolYearEnd = schoolYearEnd.withYear(thisYear); if(schoolYearStart.isBefore(schoolYearEnd)){ return new Interval(schoolYearStart, schoolYearEnd).contains(now); } else{ return !(new Interval(schoolYearEnd, schoolYearStart).contains(now)); } } 

这样,如果学年重叠2年或1年,它就可以工作。

问题不清楚

你的问题不清楚。

首先,如果用户输入的日期不是一年,那么他们就不会输入日期。 他们输入的是月份编号和月份编号。

也许问题是:

确定给定的目标日期(可能是今天)是否包含在由一对月份和月份日期数字(“月日”)定义的两个区间中的任何一个中,我们认为该对与(a)在日期中结束年或(b)从日期开始。 我们假设这对代表了不到一年的时间跨度。

乔达时间

如果这是问题,那么这里是一些使用Joda-Time 2.7的代码。 两个警告:

  • 您可以使用月份数字和日期数字的巧妙比较来做更短的事情。 但是为了具有教育意义,并为类似问题提供更多灵活性,我正在使用Joda-Timefunction。
  • 只有极少的思考和测试已进入此代码。 尝试使用各种值进行更多测试,并在生产中使用之前重新考虑逻辑。

事实certificate, Joda-Time有一个MonthDay类,正是你需要的。

 MonthDay mdStart = new MonthDay ( DateTimeConstants.NOVEMBER, 1 ); MonthDay mdStop = new MonthDay ( DateTimeConstants.JUNE, 1 ); 

请注意,时区对于确定像现在这样的时刻的月份和日期至关重要。 例如,在巴黎午夜之后的某个时刻,蒙特利尔仍然是“昨天”。 使用适当的时区名称 ,从不使用3-4个字母代码。

 DateTime target = DateTime.now ( DateTimeZone.forID ( "Africa/Casablanca" ) ); // Using current date-time, but could be any date-time. 

toDateTime方法从MonthDay (月份编号和日期编号)获取数据字段,并将它们应用于现有DateTime以创建新的DateTime实例。 Joda-Time使用不可变对象 ,因此我们基于旧实例创建一个新实例,而不是修改旧实例。

让我们考虑两个可能的跨度:

  • 其中MonthDay对对象是以目标DateTime年份结束的时间跨度。 (较早的间隔)
  • 其中MonthDay对对象是目标DateTime年开始的时间跨度。 (后来的间隔)

首先是较早的间隔。

 // Determine a span of time, moving the Start to previous year if need be. DateTime earlierStart = mdStart.toDateTime ( target ); // Overlay the data fields of the MonthDay on top of the data fields of a DateTime to create a new DateTime object. DateTime earlierStop = mdStop.toDateTime ( target ); if ( earlierStart.isAfter ( earlierStop ) ) { // If the start would land after the stop, then adjust the start to *previous* year. earlierStart = earlierStart.minusYears ( 1 ); } 

Joda-Time还提供Interval类,用于将时间跨度定义为沿时间线的一对特定点。

 Interval earlierInterval = new Interval ( earlierStart, earlierStop ); // Determine a span of time, moving the Stop to next year if need be. DateTime laterStart = mdStart.toDateTime ( target ); // Overlay the data fields of the MonthDay on top of the data fields of a DateTime to create a new DateTime object. DateTime laterStop = mdStop.toDateTime ( target ); if ( laterStart.isAfter ( laterStop ) ) { // If the start would land after the stop, then adjust the start to *next* year. laterStop = laterStop.plusYears ( 1 ); } Interval laterInterval = new Interval ( laterStart, laterStop ); 

现在测试目标DateTime是否包含在Interval 。 请注意,Joda-Time明智地使用半开放方法来定义时间跨度,其中开头是包含的,而结尾是独占的 。 所以,在我们的例子中,6月1日不在我们的范围内。 这意味着如果目标是11月1日我们会受到打击,但如果目标是6月1日,我们就会错过。

 Boolean earlierHasTarget = earlierInterval.contains ( target ); Boolean laterHasTarget = laterInterval.contains ( target ); Boolean targetContained = ( earlierHasTarget || laterHasTarget ); 

java.time

Java 8有java.time,一个受Joda-Time启发的新日期时间框架。 虽然与Joda-Time大致相似,但它不是直接的替代品。 每个都有其他缺乏的function。 特别是,java.time缺少与Interval等效的任何东西。