View Javadoc

1   /*
2    *  File: WeekIterator.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.date.iterator;
12  
13  import java.util.Calendar;
14  import java.util.GregorianCalendar;
15  
16  import de.jaret.util.date.JaretDate;
17  
18  /**
19   * Implementation of the DateIterator iterating over weeks.
20   * 
21   * @author kliem
22   * @version $Id: WeekIterator.java 828 2009-02-08 13:58:21Z kliem $
23   */
24  public class WeekIterator extends AbstractDateIterator implements DateIterator {
25      /** default formatter. */
26      protected IIteratorFormatter _defaultFormatter = new IIteratorFormatter() {
27  
28          /**
29           * {@inheritDoc}
30           */
31          public String getLabel(JaretDate date, Format format) {
32              if (format.equals(Format.SHORT)) {
33                  return Integer.toString(date.getWeekOfYear()) + ".";
34              } else if (format.equals(Format.MEDIUM)) {
35                  return Integer.toString(date.getWeekOfYear()) + ".(" + date.toDisplayStringDate() + ")";
36              } else {
37                  JaretDate nextDate = previewNextDate();
38                  return Integer.toString(date.getWeekOfYear()) + ". (" + date.toDisplayStringDate()
39                          + (nextDate != null ? "-" + nextDate.copy().backDays(1).toDisplayStringDate() : "") + ")";
40              }
41          }
42      };
43      /** number of days in a week. */
44      private static final int DAYSINWEEK = 7;
45  
46      /**
47       * {@inheritDoc}
48       */
49      protected void advanceDate(JaretDate date) {
50          date.advanceDays(DAYSINWEEK);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public long getApproxStepMilliSeconds() {
57          return 7 * 24 * 60 * 60 * 1000;
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      protected JaretDate correctStartDate(JaretDate date) {
64          date.setTime(0, 0, 0);
65  
66          Calendar cal = new GregorianCalendar();
67          int firstDayInWeek = cal.getFirstDayOfWeek();
68  
69          // search clean starting day
70          while (date.getDayOfWeek() != firstDayInWeek) {
71              date.backDays(1);
72          }
73          return date;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      protected IIteratorFormatter getDefaultFormatter() {
80          return _defaultFormatter;
81      }
82  
83  }