View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Parses the very simple schedule format.
30   * <p>
31   * @author Aaron Smuts
32   */
33  public class ScheduleParser
34  {
35      /**
36       * For each date time that is separated by a comma in the
37       * OptimizationSchedule, create a date and add it to an array of dates.
38       * <p>
39       * @param schedule
40       * @return Date[]
41       * @throws ParseException
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       * For a single string it creates a date that is the next time this hh:mm:ss
65       * combo will be seen.
66       * <p>
67       * @param startTime
68       * @return Date
69       * @throws ParseException
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          // This will result in a date of 1/1/1970
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          // if the date is less than now, add a day.
89          if ( cal.before( now ) )
90          {
91              cal.add( Calendar.DAY_OF_MONTH, 1 );
92          }
93  
94          return cal.getTime();
95      }
96  }