1 package org.jbpm.calendar; 2 3 import java.io.Serializable ; 4 import java.text.*; 5 import java.text.DateFormat ; 6 import java.util.*; 7 8 public class DayPart implements Serializable { 12 13 private static final long serialVersionUID = 1L; 14 15 int fromHour = -1; 16 int fromMinute = -1; 17 int toHour = -1; 18 int toMinute = -1; 19 Day day = null; 20 int index = -1; 21 22 public DayPart(String dayPartText, DateFormat dateFormat, Day day, int index) { 23 this.day = day; 24 this.index = index; 25 26 int separatorIndex = dayPartText.indexOf('-'); 27 if (separatorIndex==-1) throw new IllegalArgumentException ("improper format of daypart '"+dayPartText+"'"); 28 String fromText = dayPartText.substring(0, separatorIndex).trim().toLowerCase(); 29 String toText = dayPartText.substring(separatorIndex+1).trim().toLowerCase(); 30 31 try { 32 Date from = dateFormat.parse(fromText); 33 Date to = dateFormat.parse(toText); 34 35 Calendar calendar = BusinessCalendar.getCalendar(); 36 calendar.setTime(from); 37 fromHour = calendar.get(Calendar.HOUR_OF_DAY); 38 fromMinute = calendar.get(Calendar.MINUTE); 39 40 calendar.setTime(to); 41 toHour = calendar.get(Calendar.HOUR_OF_DAY); 42 toMinute = calendar.get(Calendar.MINUTE); 43 44 } catch (ParseException e) { 45 e.printStackTrace(); 46 } 47 } 48 49 public Date add(Date date, Duration duration) { 50 Date end = null; 51 52 Calendar calendar = BusinessCalendar.getCalendar(); 53 calendar.setTime(date); 54 int hour = calendar.get(Calendar.HOUR_OF_DAY); 55 int minute = calendar.get(Calendar.MINUTE); 56 57 long dateMilliseconds = ((hour*60)+minute)*60*1000; 58 long dayPartEndMilleseconds = ((toHour*60)+toMinute)*60*1000; 59 long millisecondsInThisDayPart = dayPartEndMilleseconds - dateMilliseconds; 60 61 if (duration.milliseconds <= millisecondsInThisDayPart) { 62 end = new Date( date.getTime() + duration.milliseconds); 63 } else { 64 Duration remainder = new Duration(duration.milliseconds - millisecondsInThisDayPart); 65 Date dayPartEndDate = new Date(date.getTime() + duration.milliseconds); 66 67 Object [] result = new Object [2]; 68 day.findNextDayPartStart(index+1, dayPartEndDate, result); 69 Date nextDayPartStart = (Date) result[0]; 70 DayPart nextDayPart = (DayPart) result[1]; 71 72 end = nextDayPart.add(nextDayPartStart, remainder); 73 } 74 75 return end; 76 } 77 78 public boolean isStartAfter(Date date) { 79 Calendar calendar = BusinessCalendar.getCalendar(); 80 calendar.setTime(date); 81 int hour = calendar.get(Calendar.HOUR_OF_DAY); 82 int minute = calendar.get(Calendar.MINUTE); 83 84 return ( (hour<fromHour) 85 || ( (hour==fromHour) 86 && (minute<=fromMinute) 87 ) 88 ); 89 } 90 91 92 public boolean includes(Date date) { 93 Calendar calendar = BusinessCalendar.getCalendar(); 94 calendar.setTime(date); 95 int hour = calendar.get(Calendar.HOUR_OF_DAY); 96 int minute = calendar.get(Calendar.MINUTE); 97 98 return ( ( (fromHour<hour) 99 || ( (fromHour==hour) 100 && (fromMinute<=minute) 101 ) 102 ) && 103 ( (hour<toHour) 104 || ( (hour==toHour) 105 && (minute<=toMinute) 106 ) 107 ) 108 ); 109 } 110 111 public Date getStartTime(Date date) { 112 Calendar calendar = BusinessCalendar.getCalendar(); 113 calendar.setTime(date); 114 calendar.set(Calendar.HOUR_OF_DAY, fromHour); 115 calendar.set(Calendar.MINUTE, fromMinute); 116 return calendar.getTime(); 117 } 118 } 119 | Popular Tags |