title: Why is Java Calendar saying the first Thursday of the month is Week 5?
body: <p>I am trying to figure out how to make an alarm on a specific day of the week.  Here is sample code that prints what I expect:</p>
  
  <pre><code>  Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.WEEK_OF_MONTH, 1);
    System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH)); // Week of month: 1
  </code></pre>
  
  <p>But when I add this to the code I get a result I dont understand:</p>
  
  <pre><code>  Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
    calendar.set(Calendar.WEEK_OF_MONTH, 1);
    System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH)); // Week of month: 5
  </code></pre>
  
  <p>I am trying to set an alarm for a specific day in a specific week.  The date today is June 15th, 2013.  Could someone please explain this to me.  Joda time is not an option with what I am doing, so I need to make this work with the regular Java libs.  Thanks for all the help.</p>
  
  <h3>Editor's note</h3>
  
  <p>The above code will return different results based on the date it is run, because <code>Calendar.getInstance()</code> returns a Calendar set to the current time.  For an illustration of the problem that is independent of the current time, use this:</p>
  
  <pre><code>DateFormat dateFormat = DateFormat.getDateInstance();
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(1371294000000L);
  System.out.println(dateFormat.format(calendar.getTime()));
  calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  System.out.println(dateFormat.format(calendar.getTime()));
  calendar.set(Calendar.WEEK_OF_MONTH, 1);
  System.out.println("Week of month: " + calendar.get(Calendar.WEEK_OF_MONTH)); 
  System.out.println(dateFormat.format(calendar.getTime()));
  </code></pre>
  
  <p>Which outputs something like this (at least if your default locale is using the Gregorian Calendar):</p>
  
  <pre class="lang-none prettyprint-override"><code>Jun 15, 2013
  Jun 13, 2013
  Week of month: 5
  May 30, 2013
  </code></pre>