title: best way to calculate the first- and last-day of Week, Month and Year
body: <p>He all,</p>
  
  <p>I am trying to calculate the first- and last-day of Week, Month and Year.
  For Month and Year it Works fine but week is wrong:</p>
  
  <pre class="lang-java prettyprint-override"><code>public Date[] calcDateRange(Calendar c, int day) {
      Date[] dr = new Date[2];
      // setMin
      c.set(day, c.getActualMinimum(day));
      dr[0] = c.getTime();
      // setMax
      c.set(day, c.getActualMaximum(day));
      dr[1] = c.getTime();
      return dr;
  }
  
  public void print() {
      SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
      Calendar cStart = Calendar.getInstance(Locale.GERMANY);
      cStart.setFirstDayOfWeek(Calendar.MONDAY);
      System.out.println("startdate: " + sdf.format(cStart.getTime()));
      Date[] minMaxD = calcDateRange(cStart, Calendar.DAY_OF_WEEK);
      System.out.println("start_of_week:\t" + sdf.format(minMaxD[0]) + "\nend_of_week:\t" + sdf.format(minMaxD[1]));
      minMaxD = calcDateRange(cStart, Calendar.DAY_OF_MONTH);
      System.out.println("start_of_month:\t" + sdf.format(minMaxD[0]) + "\nend_of_month:\t" + sdf.format(minMaxD[1]));
      minMaxD = calcDateRange(cStart, Calendar.DAY_OF_YEAR);
      System.out.println("start_of_year:\t" + sdf.format(minMaxD[0]) + "\nend_of_year:\t" + sdf.format(minMaxD[1]));
  }
  </code></pre>
  
  <p>Can someone help me to find my mistakes?
  What is the best way to calculate the dates?</p>
  
  <p>Output:</p>
  
  <pre><code>startdate: 20.03.2014
  start_of_week:  23.03.2014 &lt;--- wrong, should be '17.03.2014'
  end_of_week:    22.03.2014 &lt;--- wrong, should be '23.03.2014'
  start_of_month: 01.03.2014
  end_of_month:   31.03.2014
  start_of_year:  01.01.2014
  end_of_year:    31.12.2014
  </code></pre>
  
  <p>Thank you in advance.</p>
  
  <p><strong>//edit</strong></p>
  
  <p>Currently I use the following methode, but im not realy happy with it:</p>
  
  <pre><code>public Date[] calcDateRange(Calendar c, int day) {
      int fdow = c.getFirstDayOfWeek();
      c.setFirstDayOfWeek(Calendar.SUNDAY);
      Date[] dr = new Date[2];
      // setMin
      c.set(day, c.getActualMinimum(day));
      //..setSecondsMinutes
      if (day == Calendar.DAY_OF_WEEK)
          c.add(day, 1); // German Week correction
      dr[0] = c.getTime();
  
      // setMax
      c.set(day, c.getActualMaximum(day));
      //..setSecondsMinutes
      if (day == Calendar.DAY_OF_WEEK)
          c.add(day, 1); // German Week correction
      dr[1] = c.getTime();
      c.setFirstDayOfWeek(fdow);
      return dr;
  }
  </code></pre>