查找一天(例如星期日或星期一)是否在Java中的两个给定日期之间

时间:2018-02-20 22:18:39

标签: java date

例如, 如果输入是星期日,请说出天= 1。

int day = 1;

两天过去了。开始日期和结束日期。让我们说开始日期是星期一,结束日期是星期三,我想要一个方法返回false。

输入日可以是任何东西(星期一是2,星期二是3等),开始和结束日期可以是任何东西。

以下是我尝试过的片段。但这不准确。有人可以帮忙吗?

   int day = 1; 
   // Will be 2 for monday, 3 for tuesday etc. 
   // Consider that the value of day is passed as parameter

  int startDayOfWeek = startDate.getCalendar().get (Calendar.DAY_OF_WEEK);
  int endDayOfWeek = endDate.getCalendar().get (Calendar.DAY_OF_WEEK);  
  LocalDate resStartDate = startDate;

  LocalDate resEndDate = resAvailabilityDetails.endDate)
  int diffBetweenStartEndDate = 
      Days.daysBetween (resStartDate, resEndDate).getDays();
  // If the Available_on is Thursday, and if the start date and end 
  // date, inclusive, do not overlap a Thursday, then do not show the
  // record. Similarly for other days of the week
  if (diffBetweenStartEndDate < 7 && ! (day >= startDayOfWeek || day <= endDayOfWeek)) {
       continue;
  }

5 个答案:

答案 0 :(得分:1)

TL;博士

DayOfWeek

详细

你的问题不明确,所以也许我误解了。似乎序列的方面是红鲱鱼,分散注意力。对于每周的“从/开始”和“到/停止”的思考只会让人感到困惑。您似乎只关心特定日期的星期几恰好是一组所需的星期值的成员。

例如,如果您要定义“周末”,请不要说“星期五和星期五之间的日子”。星期一”。只要说“星期六和星期六”星期日”。

java.time 框架具有用于这些目的的类。使用它们。

EnumSet枚举包含七个预定义对象,每个对象用于星期一至星期日的星期几。

Set类是Set< DayOfWeek > dows = EnumSet.of( DayOfWeek.MONDAY , DayOfWeek.TUESDAY , DayOfWeek.WEDNESDAY ) ; 的实现,针对保存枚举对象进行了高度优化。这个类非常快,并且使用的内存非常少。

收集您关心的星期值。

DayOfWeek dow = myLocalDate.getDayOfWeek() ;

获取您的约会日期。

Boolean hit = dows.contains( dow ) ;

询问该集合是否包含该星期几。

DayOfWeek

在您的代码库中,传递这些Set枚举和1对象。避免使用笨拙的表示,如“Mo”之类的字符串和DayOfWeek之类的模糊整数。

您甚至不需要在问题中建议的专用方法。如上所示,您可以轻松地在一行或两行明确的显式代码中完成工作,而不是调用其他方法。

可以使用一周中某一天的想法,但我认为你的问题场景没有任何优势。

但如果你坚持,你需要明确定义本周的订购。我建议你遵循ISO 8601标准周定义,从周一开始,一直持续到周日,编号为1-7。 Set< DayOfWeek > dows = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.WEDNESDAY ) ; // 3 days, Mon/Tues/Wed. 枚举使用此标准定义。

{{1}}

答案 1 :(得分:0)

我认为我现在做对了,但也许有人喜欢复制我的测试代码来检查他自己的解决方案或改进测试:

String [] daya = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}; 
List <String> days = Arrays.asList (daya);

boolean dayInRange (String check, String from, String to) {
  int f = days.indexOf (from);
  int t = days.indexOf (to);
  int c = days.indexOf (check);
  if (f < 0 || t < 0 || c < 0)  {
    System.out.print ("  invalid date! ");
    return false; 
  }

  if (f < t) {
    System.out.print ("ordinary between");
    return (f <= c && c <= t); // ordinary ordering 
  }
  if (f == t) {
    System.out.print ("   start=end    ");
    return (f == c); // From-to is same date, 
  }
  if (f > t) {
    System.out.print ("weekend passing ");
    return (t <= c || c <= f); // range over the weekend border 
  }
  System.out.print (" no match ");
  return false;
} 


void check (boolean expected, String x, String f, String t, String msg) {
    if (expected == dayInRange (x, f, t)) 
        System.out.println ("fine +: (" + x + ") in " + f + "-"  + t + " :\t"  + msg);
    else 
        System.out.println ("bad---: (" + x + ") " + f + "-"  + t + " :\t"  + msg);
}

check (true,  "Mo", "Mo", "Th", "on left boundary");
check (true,  "Th", "Mo", "Th", "on right boundary");
check (false, "We", "Mo", "Tu", "Wednsday past Tuesday (and Mo)"); 
check (false, "Mo", "We", "Fr", "Mo before We (and Fr)") 
check (true,  "We", "Mo", "Th", "ordinary mo-_-(we)-th");
check (true,  "We", "We", "We", "one day interval, hit");
check (false, "Fr", "We", "We", "one day interval, past");
check (false, "Mo", "We", "We", "one day interval, too early");
check (true,  "We", "Fr", "Th", "every day should fit between Fr and Th") 
check (false, "Yo", "Mo", "Th", "no such date");

这显然是一个案例,Testcode非常有用。但我们也需要来自算法的反馈,以检查哪个测试产生了结果。我的第一个实现没有透露。

测试的Jshell输出:

-> check (true,  "Mo", "Mo", "Th", "on left boundary");
ordinary betweenfine +: (Mo) in Mo-Th : on left boundary

-> check (true,  "Th", "Mo", "Th", "on right boundary");
ordinary betweenfine +: (Th) in Mo-Th : on right boundary

-> check (false, "We", "Mo", "Tu", "Wednsday past Tuesday (and Mo)"); 
ordinary betweenfine +: (We) in Mo-Tu : Wednsday past Tuesday (and Mo)

-> check (false, "Mo", "We", "Fr", "Mo before We (and Fr)") 
ordinary betweenfine +: (Mo) in We-Fr : Mo before We (and Fr)

-> check (true,  "We", "Mo", "Th", "ordinary mo-_-(we)-th");
ordinary betweenfine +: (We) in Mo-Th : ordinary mo-_-(we)-th

-> check (true,  "We", "We", "We", "one day interval, hit");
   start=end    fine +: (We) in We-We : one day interval, hit

-> check (false, "Fr", "We", "We", "one day interval, past");
   start=end    fine +: (Fr) in We-We : one day interval, past

-> check (false, "Mo", "We", "We", "one day interval, too early");
   start=end    fine +: (Mo) in We-We : one day interval, too early

-> check (true,  "We", "Fr", "Th", "every day should fit between Fr and Th") 
weekend passing fine +: (We) in Fr-Th : every day should fit between Fr and Th

-> check (false, "Yo", "Mo", "Th", "no such date");
  invalid date! fine +: (Yo) in Mo-Th : no such date

答案 2 :(得分:0)

/**
 * @return true if {@code test} is between {@code start}
 *          and the following (or same) {@code end}, inclusive  
 */
public static boolean isBetween(DayOfWeek start, DayOfWeek test, DayOfWeek end) {
    // is start before end?
    if (start.getValue() < end.getValue()) {
        return start.getValue() <= test.getValue() && test.getValue() <= end.getValue();
    } else if (start.equals(end)) {
        return test.equals(start);
    } else {
        // end is in the following week
        return start.getValue() <= test.getValue() || test.getValue() <= end.getValue();
    }
} 

我正在使用DayOfWeek中的java.time,因为我总是使用java.time进行日期操作,所以这是我在这种情况下自然会遇到的类型。如果您使用的是Joda-Time,请使用int中的LocalDate.getDayOfWeek()执行类似操作。该算法对于星期日或星期一是否开始一周无关紧要,您无需关心。我建议你离开旧的Calendar课程,它已经过时了,今天我们有更好的。

答案 3 :(得分:-1)

由于您希望开始日期和结束日期都包含在内,因此差异检查应与6进行比较,因为如果结束日期是开始日期后的6天,则它们将涵盖整整一周,例如星期一 - &gt;周日。

您当然还需要确保结束日期不早于开始日期。

由于星期几是循环比例,在某个时间点(使用Calendar)之间的周六和周日之间翻转,您需要考虑到这一点。

如果是endDate&gt; startDate,但是endDayOfWeek&lt; startDayOfWeek,那么你应该在endDayOfWeek中添加1周(7天),以确保它是&gt; = startDayOfWeek。

同样适用于testDayOfWeek。

不使用if语句,总是添加7然后使用模数7,使代码更简单(更小)。

所以,你的测试是(伪代码):

if (endDate < startDate):
    return false
if (diff(startDate, endDate) >= 6)
    return true
return (testDoW + 7 - startDoW) % 7 <= (endDoW + 7 - startDoW) % 7

答案 4 :(得分:-2)

只需比较两个整数:

int day = Calendar.Sunday; 
int startDayOfWeek = startDate.getCalendar().get(Calendar.DAY_OF_WEEK);
int endDayOfWeek = endDate.getCalendar().get(Calendar.DAY_OF_WEEK); 

if(startDayOfWeek < day && endDayOfWeek > day) {
     //The given day is between the start and end days.
}