title: Telling if Two dates are a Month Apart
body: <p>I am having some slight issue with this program. I assume a month apart is 30 days in between the two dates. The program works for all the test dates except for the call monthApart(2, 14, 3, 15) which returns false instead of true. Any help would be appreciated, thanks.</p>
  
  <p>This program is supposed to tell if two dates are a month apart. It is assumed that the user inputs a valid month and day.  Write a method named monthApart that accepts four integer parameters representing two calendar dates. Each date consists of a month (1 through 12) and a day (1 through the number of days in that month [28-31]). The method returns whether the dates are at least a month apart. Assume that all dates in this problem occur during the same year. For example,</p>
  
  <blockquote>
    <p>Following dates are all considered to be at least a month apart from
    9/19 (September 19): 2/14, 7/25, 8/2, 8/19, 10/19, 10/20, and 11/5. </p>
    
    <p>The following dates are NOT at least a month apart from 9/19: 9/20,
    9/28, 10/1, 10/15, and 10/18.</p>
  </blockquote>
  
  <p>Note that the first date could come before or after (or be the same as) the second date. Assume that all parameter values passed are valid.</p>
  
  <p>Answers to the problem:
           Call:                          Answer: Result:</p>
  
  <p>1   monthApart(6, 14, 9, 21)    true    true    </p>
  
  <p>2   monthApart(4, 5, 5, 15)     true    true    </p>
  
  <p>3   monthApart(4, 15, 5, 15)    true    true    </p>
  
  <p>4   monthApart(4, 16, 5, 15)    false   false   </p>
  
  <p>5   monthApart(6, 14, 6, 8)     false   false   </p>
  
  <p>6   monthApart(7, 7, 6, 8)      false   false   </p>
  
  <p>7   monthApart(7, 8, 6, 8)      true    true    </p>
  
  <p>8   monthApart(10, 14, 7, 15)   true    true    </p>
  
  <p>9   monthApart(2, 14, 3, 15)    true    false   </p>
  
  <p>10  monthApart(1, 1, 12, 31)    true    true     </p>
  
  <p>Sample calls:</p>
  
  <blockquote>
    <p>Call  Returns     Because monthApart( 6, 14, 9, 21)   true    June 14th is
    at least a month before September 21st</p>
    
    <p>monthApart( 4, 5, 5, 15)  true    April 5th is at least a month before
    May 15th</p>
    
    <p>monthApart( 4, 15, 5, 15)     true    April 15th is at least a month before
    May 15th</p>
    
    <p>monthApart( 4, 16, 5, 15)     false   April 16th is NOT at least a month
    before May 15th</p>
    
    <p>monthApart( 6, 14, 6, 8)  false   June 14th is NOT at least a month
    apart from June 8th</p>
    
    <p>monthApart( 7, 7, 6, 8)   false   July 7th is NOT at least a month apart
    from June 8th</p>
    
    <p>monthApart( 7, 8, 6, 8)   true    July 8th is at least a month after June
    8th</p>
    
    <p>monthApart( 10, 14, 7, 15)    true    October 14th is at least a month
    after July 15th</p>
  </blockquote>
  
  <p>My code is messy right now; I'll clean it up later.</p>
  
  <pre><code>public static boolean monthApart(int monthOne, int dayOne, int monthTwo, int dayTwo){
      int monthDays = -1; 
      int monthMin = Math.min(monthOne, monthTwo); // Gets the lesser month
  
      if (monthOne&lt;1 || monthOne &gt;12 || monthTwo&gt;12 || monthOne&lt;1) { // If not a possible month
          throw new IllegalArgumentException("Out of range");
      }
  
      switch(monthMin) { // Gets the corresponding last day of month
          case 1:
              monthDays = 31;
              break;
          case 2:
              monthDays = 28;
              break;
          case 3:
              monthDays = 31;
              break;
          case 4:
              monthDays = 30;
              break;
          case 5:
              monthDays = 31;
              break;
          case 6:
              monthDays = 30;
              break;
          case 7:
              monthDays = 31;
              break;
          case 8: 
              monthDays = 31;
              break;
          case 9:
              monthDays = 30;
              break;
          case 10:
              monthDays = 31;
              break;
          case 11:
              monthDays = 30;
              break;
          case 12:
              monthDays = 31;
              break;
      }
  
      int dayFirst = -1; // Plan: Last day of month-day of lower month + days on next month
      int daySecond = -1;
      if (monthOne&lt;monthTwo) {
          dayFirst = dayOne;
          daySecond = dayTwo;
      } else if (monthOne&gt;monthTwo) {
          dayFirst = dayTwo;
          daySecond = dayOne;
      }
  
      if (Math.abs(monthOne-monthTwo)&gt;1) { // More than a month apart
          return true;
      } else if (monthOne==monthTwo) { // Same month
          return false;
      } else if (Math.abs(monthOne-monthTwo)==1) { // Adjacent months
  
          if (((monthDays-dayFirst) + daySecond) &lt; 30 ) { // Gets difference in days
              return false;
          } else {
              return true;
          }
      }
      return false;
  }
  </code></pre>