KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > calendar > Holiday


1 package org.jbpm.calendar;
2
3 import java.io.Serializable JavaDoc;
4 import java.text.*;
5 import java.util.*;
6 import java.util.List JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 /**
10  * identifies a continuous set of days.
11  */

12 public class Holiday implements Serializable JavaDoc {
13   
14   private static final long serialVersionUID = 1L;
15   
16   Date fromDay = null;
17   Date toDay = null;
18   BusinessCalendar businessCalendar = null;
19
20   public static List JavaDoc parseHolidays(Properties JavaDoc calendarProperties, BusinessCalendar businessCalendar) {
21     List JavaDoc holidays = new ArrayList();
22     
23     DateFormat dateFormat = new SimpleDateFormat(calendarProperties.getProperty("day.format"));
24     Iterator iter = calendarProperties.keySet().iterator();
25     while (iter.hasNext()) {
26       String JavaDoc key = (String JavaDoc) iter.next();
27       if (key.startsWith("holiday")) {
28         Holiday holiday = new Holiday(calendarProperties.getProperty(key), dateFormat, businessCalendar);
29         holidays.add(holiday);
30       }
31     }
32
33     return holidays;
34   }
35
36   public Holiday(String JavaDoc holidayText, DateFormat dateFormat, BusinessCalendar businessCalendar) {
37     this.businessCalendar = businessCalendar;
38     try {
39       int separatorIndex = holidayText.indexOf('-');
40       if (separatorIndex==-1) {
41         fromDay = dateFormat.parse(holidayText.trim());
42         toDay = fromDay;
43       } else {
44         String JavaDoc fromText = holidayText.substring(0, separatorIndex).trim();
45         String JavaDoc toText = holidayText.substring(separatorIndex+1).trim();
46         fromDay = dateFormat.parse(fromText);
47         toDay = dateFormat.parse(toText);
48       }
49       // now we are going to set the toDay to the end of the day, rather then the beginning.
50
// we take the start of the next day as the end of the toDay.
51
Calendar calendar = BusinessCalendar.getCalendar();
52       calendar.setTime(toDay);
53       calendar.add(Calendar.DATE, 1);
54       toDay = calendar.getTime();
55       
56     } catch (ParseException e) {
57       throw new RuntimeException JavaDoc("couldn't parse holiday '"+holidayText+"'", e);
58     }
59   }
60
61   public boolean includes(Date date) {
62     return ( (fromDay.getTime()<=date.getTime())
63              && (date.getTime()<toDay.getTime())
64            );
65   }
66 }
67
Popular Tags