View Javadoc

1   /*
2    *  File: DateUtils.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;
12  
13  import java.util.Calendar;
14  
15  import de.jaret.util.date.holidayenumerator.HolidayEnumerator;
16  
17  /**
18   * Date util class containing methods for JaretDate and the HolidayEnumerator.
19   * 
20   * @author Peter Kliem
21   * @version $Id: DateUtils.java 262 2007-02-17 23:51:03Z olk $
22   */
23  public class DateUtils {
24      /**
25       * Count the working days between from and to.
26       * 
27       * @param from start date. Will be included!
28       * @param to end date. will be included.
29       * @param he holidayenumerator for determinig if a day is a holiday
30       * @return number of working days
31       */
32      public static int workingDays(JaretDate from, JaretDate to, HolidayEnumerator he) {
33          int count = 0;
34  
35          JaretDate d = from.copy();
36          while (d.compareDateTo(to) <= 0) {
37              if (!d.isWeekendDay() && !he.isHoliday(d.getDate())) {
38                  count++;
39              }
40              d.advanceDays(1);
41          }
42  
43          return count;
44      }
45  
46      /** cache for the first day of the week. */
47      private static int _firstDayofWeek = -1;
48      /**
49       * Retrieve the first day of the week using the systems default locale.
50       * 
51       * @return the first day of the week as defiend in gregoriean calendar for the current default locale.
52       */
53      public static int getFirstDayOfWeek() {
54          if (_firstDayofWeek != -1) {
55              return _firstDayofWeek;
56          }
57          Calendar cal = Calendar.getInstance();
58          _firstDayofWeek = cal.getFirstDayOfWeek();
59          return _firstDayofWeek;
60      }
61      
62      
63      
64  }