【问题标题】:Java How do I get if a date is before the first Sunday of November?Java 如果日期早于 11 月的第一个星期日,我如何获得?
【发布时间】:2013-10-12 21:48:03
【问题描述】:

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

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 月的第一个星期日

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

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

【问题讨论】:

  • 你也需要年份来确定
  • 我不会将其标记为重复!他的代码有问题!我们应该帮助他,重复的根本没有回答他的问题!
  • 我不明白代码特定问题如何总是被标记为重复。我怀疑这段代码之前已经发布过...... .
  • @mvp 当 OP 学习编写自己的代码时,如果一切都由库提供??

标签: java date timezone dst


【解决方案1】:

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

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

【讨论】:

  • 是的.. 但有时人们编写代码是为了练习而不是真正拥有应用程序。这并没有解决他的问题,而是提出了另一种现成的解决方案。
【解决方案2】:

请看我的内联 cmets

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;    
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2011-12-05
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多