Java 如何检查一天是否在一个范围内?

Java 如何检查一天是否在一个范围内?,java,algorithm,math,calendar,dayofweek,Java,Algorithm,Math,Calendar,Dayofweek,我有一份工作,从给定的一天到一周中的某一天。例如(星期一至星期六)。 我能够在stopday>starday(例如startday->Monday stopday->Saturday)中涵盖该案例 但是,当范围变为像星期三到星期一这样的时候,我无法涵盖这个案例 private boolean isNotWindow(DateTime todayDate) { final int hr = 3600; final int min = 60; int

我有一份工作,从给定的一天到一周中的某一天。例如(星期一至星期六)。 我能够在stopday>starday(例如startday->Monday stopday->Saturday)中涵盖该案例 但是,当范围变为像星期三到星期一这样的时候,我无法涵盖这个案例

private boolean isNotWindow(DateTime todayDate) {
        final int hr = 3600;
        final int min = 60;
        int stopDay =
                Integer.parseInt(ShipmentTrackingEmailProperties.getInstance().getProperty(
                        ShipmentTrackingEmailProperties.SHIPMENT_EMAIL_STOP_DAY));
        int startDay =
                Integer.parseInt(ShipmentTrackingEmailProperties.getInstance().getProperty(
                        ShipmentTrackingEmailProperties.SHIPMENT_EMAIL_START_DAY));

        if(stopDay<startDay)
        {
            //Not able to figure out??????
        }
        if (todayDate.getDayOfWeek() >= stopDay) {
            String stopTime =
                    ShipmentTrackingEmailProperties.getInstance().getProperty(ShipmentTrackingEmailProperties.SHIPMENT_EMAIL_STOP_TIME);
            String[] array = stopTime.split(":");
            System.out.println(array[0] + "  " + array[1] + "   " + array[2]);
            int hh = Integer.parseInt(array[0]);
            int mm = Integer.parseInt(array[1]);
            int ss = Integer.parseInt(array[2]);
            int tSec = todayDate.getHourOfDay() * hr + todayDate.getMinuteOfDay() * min + todayDate.getSecondOfDay();
            int sSec = hh * hr + mm * min + ss;
            if (tSec > sSec) {
                     return true;
            }
        }
        if (todayDate.getDayOfWeek() <= startDay) {
            String startTime =
                    ShipmentTrackingEmailProperties.getInstance().getProperty(ShipmentTrackingEmailProperties.SHIPMENT_EMAIL_START_TIME);
            String[] array = startTime.split(":");
            int hh = Integer.parseInt(array[0]);
            int mm = Integer.parseInt(array[1]);
            int ss = Integer.parseInt(array[2]);
            int tSec = todayDate.getHourOfDay() * hr + todayDate.getMinuteOfDay() * min + todayDate.getSecondOfDay();
            int sSec = hh * hr + mm * min + ss;
            if (tSec <= sSec) {
                LOG.info("Not a valid day to send mail ." + todayDate.getDayOfWeek());
                 return true;
            }
        }
             LOG.info("Valid day to send mail ." + todayDate.getDayOfWeek());
         return false;
    }
private boolean isNotWindow(DateTime todayDate){
最终int hr=3600;
最终int最小值=60;
int stopDay=
整数.parseInt(ShipmentTrackingEmailProperties.getInstance().getProperty(
ShipmentTrackingEmailProperties.Shipping_EMAIL_STOP_DAY));
国际起始日=
整数.parseInt(ShipmentTrackingEmailProperties.getInstance().getProperty(
ShipmentTrackingEmailProperties.Shipping_EMAIL_START_DAY));
如果(停止日=停止日){
串停止时间=
ShipmentTrackingEmailProperties.getInstance().getProperty(ShipmentTrackingEmailProperties.Shipping\u EMAIL\u STOP\u TIME);
String[]数组=stopTime.split(“:”);
System.out.println(数组[0]+“”+数组[1]+“”+数组[2]);
inthh=Integer.parseInt(数组[0]);
int mm=Integer.parseInt(数组[1]);
int ss=Integer.parseInt(数组[2]);
int tSec=todayDate.getHourOfDay()*hr+todayDate.getMinuteOfDay()*min+todayDate.getSecondOfDay();
int sSec=hh*hr+mm*min+ss;
如果(tSec>sSec){
返回true;
}
}

如果(todayDate.getDayOfWeek()您可以使用此函数检查范围内的日期

private static boolean inRange(int startDay, int stopDay, int checkMe) {
    if(startDay==stopDay) {
           if(startDay==checkMe){
                   return true;
           } else {
                   return false;
           }
    }
    while(startDay!=stopDay) {
        if(startDay==checkMe || stopDay==checkMe) {
            return true;
        }
        if(startDay==7) {
            startDay =0;
        }
        startDay++;
    }
    return false;
}
希望有帮助。

您需要:

if startday<stopday then
    if the day is in the interval (startday,stopday) then OK
    else NotOk
else 
    if the day is not in the interval (startday,stopday) then OK
    else NotOk

我没有看过你的代码。但是,我建议你尝试一下joda time API。我有很多用于日期相关任务的易于使用的函数。Chenqui.使用
JodaTime
是一个选项吗?如果是,你可以定义一个
Interval
并检查它是否包含你的日期。下面是一篇so帖子,告诉我们如何使用joda-这是一个非常简单的日期m操作是可以的。但是如何处理天数和时间,并检查某一天的时间是否在某一范围内。@BoratSagdiyev-我想你理解错了这个问题。我有一天(一周中的一天)还有一个时间,我想知道它是否在两个给定的星期和时间内。这是正确的,先生,但是你能建议在我的代码中进行编辑吗。这会很有帮助。谢谢。你可以使用inRange函数来确定你的一天是否在startday和stopday范围内。如果它在day范围内,你可以计算开始时间和结束时间,看看今天是否在ate的时间是否在这个范围内。我添加的唯一一件事是我还必须考虑到时间,所以我将一天中的秒数计算为(dayOfWeek-1)*24*3600+秒数,并将它们与日进行比较。@RamanSingh当然。我只想展示主要思想……我喜欢基于计数的逻辑:-)
If((day-startday)*(stopday-day)*(stopday-startday)>=0) then OK
else NotOk