0

I am doing a compound if statement that calculates whether a certain date is a daylight savings date, but I have become stuck when trying to find a way to calculate if the date is before the first Sunday of November or if it is after. Could someone help me? Here is my code right now:

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

Edit: I believe that my question wasn't clear enough. I know how to find the first Sunday of November

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

what I seem to be unable to do is find whether my date is before or after this first Sunday of November. And I need to do it using an if statement, not preloaded libraries, methods or classes.

alessfg
  • 37
  • 6
  • 2
    possible duplicate of [Determine Whether Daylight Savings Time (DST) is Active in Java for a Specified Date](http://stackoverflow.com/questions/1060479/determine-whether-daylight-savings-time-dst-is-active-in-java-for-a-specified) – mvp Oct 06 '13 at 06:00
  • You need year as well to determine – jmj Oct 06 '13 at 06:00
  • 2
    I wouldn't flag it as duplicate! He has an issue in his code! We should help him in that, the duplicate **DOESN'T** answer his question at all! – Maroun Oct 06 '13 at 06:03
  • 1
    I don't get how code specific questions always get flagged as duplicate. I doubt this code has been posted before... . – BlackHatSamurai Oct 06 '13 at 06:03
  • OP approach is simply wrong. Linked duplicate solves it correctly – mvp Oct 06 '13 at 06:04
  • 1
    @mvp If everything provides by library when OP learns to write his own code ?? – Suresh Atta Oct 06 '13 at 06:06
  • if approach was correct, sure. But it is not. Correct way is `TimeZone.getDefault().inDaylightTime(date)` – mvp Oct 06 '13 at 06:07
  • I must use if statements to do it – alessfg Oct 06 '13 at 06:16

2 Answers2

0

Instead of re-inventing the wheel, I would suggest you to use joda time, which takes daylight savings time (DST) into account.

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

Mingyu
  • 26,145
  • 13
  • 50
  • 58
  • 2
    Yes.. but sometimes people write code to practice and not to really have an application. This doesn't solve his problem, but rather suggests an alternative ready solution. – Maroun Oct 06 '13 at 06:01
0

Please see my inline comments

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;    
 }
}
Ravi Vasamsetty
  • 403
  • 2
  • 6