1 package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.StringTokenizer;
27
28
29
30
31
32
33 public class ScheduleParser
34 {
35
36
37
38
39
40
41
42
43 public static Date[] createDatesForSchedule( String schedule )
44 throws ParseException
45 {
46 if ( schedule == null )
47 {
48 throw new ParseException( "Cannot create schedules for a null String.", 0 );
49 }
50
51 StringTokenizer toker = new StringTokenizer( schedule, "," );
52 Date[] dates = new Date[toker.countTokens()];
53 int cnt = 0;
54 while ( toker.hasMoreTokens() )
55 {
56 String time = toker.nextToken();
57 dates[cnt] = getDateForSchedule( time );
58 cnt++;
59 }
60 return dates;
61 }
62
63
64
65
66
67
68
69
70
71 public static Date getDateForSchedule( String startTime )
72 throws ParseException
73 {
74 if ( startTime == null )
75 {
76 throw new ParseException( "Cannot create date for a null String.", 0 );
77 }
78
79 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
80 Date date = sdf.parse(startTime);
81 Calendar cal = Calendar.getInstance();
82
83 cal.setTime(date);
84
85 Calendar now = Calendar.getInstance();
86 cal.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
87
88
89 if ( cal.before( now ) )
90 {
91 cal.add( Calendar.DAY_OF_MONTH, 1 );
92 }
93
94 return cal.getTime();
95 }
96 }