如何检查当前日期是否在java中两个重复发生的日期之间? [英] How to check if current date is between two reoccurring dates in java?

查看:208
本文介绍了如何检查当前日期是否在java中两个重复发生的日期之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create an app, and got stuck at calculating if today is in the school year. The user enters two dates, with no year, that reoccur annually. These are the start and end dates of the school year.

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

I want to check if the current date, is between these two, even if it overlaps two years. So if school starts on November, and ends on june, if today is January 22nd, it should return true. But if its july, it should return false.

我确实发现了这个问题: Php--计算学年是什么时间,但它适用于学年,没有假期。

I did find this question: Php - work out what academic year it is, But it works on academic years, which don't have a holiday.

BTW我有joda时间,如果有帮助。

BTW I have joda time, if that helps.

提前致谢。

推荐答案

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

Well, I thought for a bit and found a few solutions, but this seemed simplest.

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。

This way it works both if the school year overlaps 2 years or 1.

这篇关于如何检查当前日期是否在java中两个重复发生的日期之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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