| 1 |
package org.apache.commons.jcs3.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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class ScheduleParser |
| 31 |
{ |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static Date[] createDatesForSchedule( final String schedule ) |
| 41 |
throws ParseException |
| 42 |
{ |
| 43 |
if (schedule == null || schedule.isEmpty()) |
| 44 |
{ |
| 45 |
throw new ParseException( "Cannot create schedules for a null or empty String.", 0 ); |
| 46 |
} |
| 47 |
|
| 48 |
final String timeStrings[] = schedule.split("\\s*,\\s*"); |
| 49 |
final Date[] dates = new Date[timeStrings.length]; |
| 50 |
int cnt = 0; |
| 51 |
for (String time : timeStrings) |
| 52 |
{ |
| 53 |
dates[cnt++] = getDateForSchedule(time); |
| 54 |
} |
| 55 |
return dates; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static Date getDateForSchedule( final String startTime ) |
| 67 |
throws ParseException |
| 68 |
{ |
| 69 |
if ( startTime == null ) |
| 70 |
{ |
| 71 |
throw new ParseException( "Cannot create date for a null String.", 0 ); |
| 72 |
} |
| 73 |
|
| 74 |
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); |
| 75 |
final Date date = sdf.parse(startTime); |
| 76 |
final Calendar cal = Calendar.getInstance(); |
| 77 |
|
| 78 |
cal.setTime(date); |
| 79 |
|
| 80 |
final Calendar now = Calendar.getInstance(); |
| 81 |
cal.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH)); |
| 82 |
|
| 83 |
|
| 84 |
if ( cal.before( now ) ) |
| 85 |
{ |
| 86 |
cal.add( Calendar.DAY_OF_MONTH, 1 ); |
| 87 |
} |
| 88 |
|
| 89 |
return cal.getTime(); |
| 90 |
} |
| 91 |
} |