001package org.apache.commons.jcs3.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;
026
027/**
028 * Parses the very simple schedule format.
029 */
030public class ScheduleParser
031{
032    /**
033     * For each date time that is separated by a comma in the
034     * OptimizationSchedule, create a date and add it to an array of dates.
035     * <p>
036     * @param schedule
037     * @return Date[]
038     * @throws ParseException
039     */
040    public static Date[] createDatesForSchedule( final String schedule )
041        throws ParseException
042    {
043        if (schedule == null || schedule.isEmpty())
044        {
045            throw new ParseException( "Cannot create schedules for a null or empty String.", 0 );
046        }
047
048        final String timeStrings[] = schedule.split("\\s*,\\s*");
049        final Date[] dates = new Date[timeStrings.length];
050        int cnt = 0;
051        for (String time : timeStrings)
052        {
053            dates[cnt++] = getDateForSchedule(time);
054        }
055        return dates;
056    }
057
058    /**
059     * For a single string it creates a date that is the next time this hh:mm:ss
060     * combo will be seen.
061     * <p>
062     * @param startTime
063     * @return Date
064     * @throws ParseException
065     */
066    public static Date getDateForSchedule( final String startTime )
067        throws ParseException
068    {
069        if ( startTime == null )
070        {
071            throw new ParseException( "Cannot create date for a null String.", 0 );
072        }
073
074        final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
075        final Date date = sdf.parse(startTime);
076        final Calendar cal = Calendar.getInstance();
077        // This will result in a date of 1/1/1970
078        cal.setTime(date);
079
080        final Calendar now = Calendar.getInstance();
081        cal.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
082
083        // if the date is less than now, add a day.
084        if ( cal.before( now ) )
085        {
086            cal.add( Calendar.DAY_OF_MONTH, 1 );
087        }
088
089        return cal.getTime();
090    }
091}