Java如果日期是在11月的第一个星期日之前我该怎么办?

Java How do I get if a date is before the first Sunday of November?

我正在做一个复合if语句来计算某个日期是否是夏令时,但是当我试图找到一种方法来计算日期是在11月的第一个星期日之前还是在它之后时,我已经陷入困境。 有人能帮助我吗?
这是我现在的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Lab5 {

/**
* Return true if the given date/time is daylight savings.
* Daylight savings time begins 2am the second Sunday of March and ends 2am the first     Sunday of November.
*
* @param month - represents the month with 1 = January, 12 = December
* @param date - represents the day of the month, between 1 and 31
* @param day - represents the day of the week with 1 = Sunday, 7 = Saturday
* @param hour - represents the hour of the day with 0 = midnight, 12 = noon
*
* Precondition: the month is between 1 and 12, the date is between 1 and 31, the day is between 1 and 7
*                and the hour is between 0 and 23.
*/

public static boolean isDayLightSavings (int month, int date, int day, int hour) {
  if (month == 1 || month == 2 || month == 12)
    return false;
  else if (month == 11) {
    if (day == 1 && hour < 2 && date < 8)
      return true;
    else            
      return false;
  }
  else
    return true;    
 }
}

编辑:我认为我的问题不够明确。 我知道如何找到11月的第一个星期天

1
2
else if (month == 11) {
if (day == 1 && hour < 2 && date < 8)

我似乎无法做的是查看我的约会是在11月的第一个星期日之前还是之后。 我需要使用if语句,而不是预加载的库,方法或类。


请参阅我的内联评论

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Lab5 {

/**
* Return true if the given date/time is daylight savings.
* Daylight savings time begins 2am the second Sunday of March and ends 2am the first     Sunday of November.
*
* @param month - represents the month with 1 = January, 12 = December
* @param date - represents the day of the month, between 1 and 31
* @param day - represents the day of the week with 1 = Sunday, 7 = Saturday
* @param hour - represents the hour of the day with 0 = midnight, 12 = noon
*
* Precondition: the month is between 1 and 12, the date is between 1 and 31, the day is between 1 and 7
*                and the hour is between 0 and 23.
*/

public static boolean isDayLightSavings (int month, int date, int day, int hour) {
  if (month == 1 || month == 2 || month == 12)
    return false;
  else if (month == 11) {
    if (date > 7)  // after 7th, it would be second week'day' of the month
      return false;
    else if ((date - day) >= 0) {
      // As we ruled out all dates above 7, we would get only 1-7
      // here. Now, lets take 3rd as Monday, date == 3, day == 2
      // so we know that 2 is Sunday. that means if date - day is positive or zero,
      // Sunday is already past. one border case is, when Sunday falls on the date entered and we
      // we need to consider the first 2 hours of the day
      if((day == 1) && (hours < 2))
        return true;
      else
        return false;
    }
    else {
      // we will come here if date - day is less than zero
      // example, 3rd came on Thursday, 3 - 5 = -2 and so
      // first sunday is not yet past
      return true;
    }            

  }
  else
    return true;    
 }
}

我建议您使用joda time,而不是重新发明轮子,这需要考虑夏令时(DST)。

http://joda-time.sourceforge.net/faq.html