1 21 package com.essiembre.library.scheduler; 22 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 27 32 public final class SchedulerUtil { 33 34 35 private final static long SECOND = 1000L; 36 37 private final static long MINUTE = 60 * SECOND; 38 39 private final static long HOUR = 60 * MINUTE; 40 41 private final static long DAY = 24 * HOUR; 42 43 private final static Map TIME_UNIT_MAP = new HashMap (); 44 static { 45 TIME_UNIT_MAP.put(new Character ('s'), new Long (SECOND)); 46 TIME_UNIT_MAP.put(new Character ('m'), new Long (MINUTE)); 47 TIME_UNIT_MAP.put(new Character ('h'), new Long (HOUR)); 48 TIME_UNIT_MAP.put(new Character ('d'), new Long (DAY)); 49 } 50 51 54 private SchedulerUtil() { 55 super(); 56 } 57 58 80 public static long parseTime(String time){ 81 82 long longTime = 0L; 83 StringBuffer number = new StringBuffer ("0"); 84 for(int i = 0; i < time.length(); i++) { 85 char ch = time.charAt(i); 86 if (Character.isDigit(ch)) { 87 number.append(ch); 88 } else { 89 Character unitType = new Character (ch); 90 if (TIME_UNIT_MAP.containsKey(unitType)) { 91 longTime += Long.parseLong(number.toString()) 92 * ((Long ) TIME_UNIT_MAP.get(unitType)).longValue(); 93 number = new StringBuffer ("0"); 94 } else { 95 throw new NumberFormatException ( 96 "\"" + ch + "\" is not a valid time unit type."); 97 } 98 } 99 } 100 longTime += Long.parseLong(number.toString()); 102 return longTime; 103 } 104 105 } 106 | Popular Tags |