001package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.text.ParseException; 023import java.text.SimpleDateFormat; 024import java.util.Calendar; 025import java.util.Date; 026import java.util.StringTokenizer; 027 028/** 029 * Parses the very simple schedule format. 030 * <p> 031 * @author Aaron Smuts 032 */ 033public class ScheduleParser 034{ 035 /** 036 * For each date time that is separated by a comma in the 037 * OptimizationSchedule, create a date and add it to an array of dates. 038 * <p> 039 * @param schedule 040 * @return Date[] 041 * @throws ParseException 042 */ 043 public static Date[] createDatesForSchedule( String schedule ) 044 throws ParseException 045 { 046 if ( schedule == null ) 047 { 048 throw new ParseException( "Cannot create schedules for a null String.", 0 ); 049 } 050 051 StringTokenizer toker = new StringTokenizer( schedule, "," ); 052 Date[] dates = new Date[toker.countTokens()]; 053 int cnt = 0; 054 while ( toker.hasMoreTokens() ) 055 { 056 String time = toker.nextToken(); 057 dates[cnt] = getDateForSchedule( time ); 058 cnt++; 059 } 060 return dates; 061 } 062 063 /** 064 * For a single string it creates a date that is the next time this hh:mm:ss 065 * combo will be seen. 066 * <p> 067 * @param startTime 068 * @return Date 069 * @throws ParseException 070 */ 071 public static Date getDateForSchedule( String startTime ) 072 throws ParseException 073 { 074 if ( startTime == null ) 075 { 076 throw new ParseException( "Cannot create date for a null String.", 0 ); 077 } 078 079 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 080 Date date = sdf.parse(startTime); 081 Calendar cal = Calendar.getInstance(); 082 // This will result in a date of 1/1/1970 083 cal.setTime(date); 084 085 Calendar now = Calendar.getInstance(); 086 cal.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH)); 087 088 // if the date is less than now, add a day. 089 if ( cal.before( now ) ) 090 { 091 cal.add( Calendar.DAY_OF_MONTH, 1 ); 092 } 093 094 return cal.getTime(); 095 } 096}