title: How to count Sundays between specific two dates in Java
body: <p>I am struggling to count the number of Sundays on the 1st of the months.
  like input 1 (test case) 1900 1 1 1902 1 1(year, month, day) the output should be 4. Start date is included.</p>
  <p>EXPLANATION:</p>
  <pre><code>1 April 1900
  1 July 1900
  1 September 1901
  1 December 1901
  </code></pre>
  <p>However, when I tried THIS:</p>
  <pre><code>6 4699 12 12 4710 1 1 1988 3 25 1989 7 13 1924 6 6 1925 6 16 1000000000000 2 2 1000000001000 3 2 1925 6 16 1924 6 6 1905 1 1 1905 1 1
  </code></pre>
  <p>OUTPUT SHOULD BE: <code>18 2 2 1720 0 1</code></p>
  <p>My CODE OUTPUT IS: <code>18 2 2 **1714** 0 1</code></p>
  <p>In the test input 6 means 6 test cases. 4699 years 12 month, 12 days, so Dec 12, 4699; 4710 end date and so on.</p>
  <p>Could you help me to solve this problem?</p>
  <pre><code>import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.*;
  import java.util.Calendar;
  
  public class nineteen {
      
      public static void main(String[] args) throws ParseException {
          SimpleDateFormat formatter = new SimpleDateFormat(&quot;MM/dd/yyyy&quot;);
          Scanner sc = new Scanner(System.in);
          int loop = sc.nextInt();
          for (int i = 0; i &lt; loop; i++) {
              long year = sc.nextLong(), month = sc.nextLong(), day = sc.nextLong(), yearto = sc.nextLong(),
                      monthto = sc.nextLong(), dayto = sc.nextLong();
              String day1 = String.valueOf(day);
              String month1 = String.valueOf(month);
              String year1 = String.valueOf(year);
              String dayt = String.valueOf(dayto);
              String montht = String.valueOf(monthto);
              String yeart = String.valueOf(yearto);
              String input_date = month1 + &quot;/&quot; + day1 + &quot;/&quot; + year1; // month day year
              String out_date = montht + &quot;/&quot; + dayt + &quot;/&quot; + yeart; // month day year
              long count = 0;
  
              Date d1 = formatter.parse(input_date);
              Date d2 = formatter.parse(out_date);
              count = saturdayscount(d1, d2);
  
              // TODO Auto-generated method stub
  
          }
          sc.close();
      }
  
      public static long saturdayscount(Date d1, Date d2) {
          Calendar c1 = Calendar.getInstance();
          c1.setTime(d1);
  
          Calendar c2 = Calendar.getInstance();
          c2.setTime(d2);
  
          long sundays = 0;
  
          while (!c1.after(c2)) {
  
              if (c1.get(Calendar.DAY_OF_MONTH) != 1) {
                  c1.add(Calendar.MONTH, 1);
                  c1.set(Calendar.DAY_OF_MONTH, 1);
  
              }
  
              if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
  
                  sundays++;
              }
  
              c1.add(Calendar.MONTH, 1);
              c1.set(Calendar.DAY_OF_MONTH, 1);
          }
  
          System.out.println(sundays);
          return sundays;
      }
  
  }
  </code></pre>