1 37 package net.sourceforge.cruisecontrol.util; 38 39 import java.text.DateFormat ; 40 import java.text.ParseException ; 41 import java.text.SimpleDateFormat ; 42 import java.util.Calendar ; 43 import java.util.Date ; 44 import java.util.TimeZone ; 45 46 import net.sourceforge.cruisecontrol.CruiseControlException; 47 48 import org.apache.log4j.Logger; 49 50 public final class DateUtil { 51 52 private static final Logger LOG = Logger.getLogger(DateUtil.class); 53 54 public static final transient long ONE_SECOND = 1000; 55 56 public static final long ONE_MINUTE = 60 * ONE_SECOND; 57 58 static final long ONE_HOUR = 60 * ONE_MINUTE; 59 60 public static final String SIMPLE_DATE_FORMAT = "yyyyMMddHHmmss"; 61 62 65 private static final String CVS_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss 'GMT'"; 69 70 private static final TimeZone UTC = TimeZone.getTimeZone("Etc/UTC"); 72 73 private DateUtil() { 74 } 75 76 83 public static int getTimeFromDate(Date date) { 84 Calendar calendar = Calendar.getInstance(); 85 calendar.setTime(date); 86 int hour = calendar.get(Calendar.HOUR_OF_DAY) * 100; 87 int minute = calendar.get(Calendar.MINUTE); 88 return hour + minute; 89 } 90 91 94 public static long milliTimeDiffernce(int earlier, int later) { 95 return milliTimeDifference(earlier, later); 96 } 97 98 107 public static long milliTimeDifference(int earlier, int later) { 108 long earlierMillis = convertToMillis(earlier); 109 long laterMillis = convertToMillis(later); 110 return laterMillis - earlierMillis; 111 } 112 113 120 public static long convertToMillis(int hhmm) { 121 int minutes = hhmm % 100; 122 int hours = (hhmm - minutes) / 100; 123 return hours * ONE_HOUR + minutes * ONE_MINUTE; 124 } 125 126 131 public static String formatTime(long time) { 132 long seconds = time / 1000; 133 long hours = seconds / 3600; 134 long minutes = (seconds % 3600) / 60; 135 seconds = seconds % 60; 136 137 StringBuffer sb = new StringBuffer (); 138 if (hours != 0) { 139 sb.append(hours).append(" hours "); 140 } 141 if (minutes != 0) { 142 sb.append(minutes).append(" minutes "); 143 } 144 if (seconds != 0) { 145 sb.append(seconds).append(" seconds"); 146 } 147 148 return sb.toString(); 149 } 150 151 154 public static Date getMidnight() { 155 Calendar midnight = Calendar.getInstance(); 156 midnight.set(Calendar.HOUR_OF_DAY, 0); 157 midnight.set(Calendar.MINUTE, 0); 158 midnight.set(Calendar.SECOND, 0); 159 midnight.set(Calendar.MILLISECOND, 0); 160 return midnight.getTime(); 161 } 162 163 public static String getFormattedTime(Date date) { 164 if (date == null) { 165 return null; 166 } 167 return new SimpleDateFormat (SIMPLE_DATE_FORMAT).format(date); 168 } 169 170 public static Date parseFormattedTime(String timeString, String description) throws CruiseControlException { 171 172 Date date; 173 if (timeString == null) { 174 throw new IllegalArgumentException ("Null date string for " + description); 175 } 176 try { 177 date = new SimpleDateFormat (SIMPLE_DATE_FORMAT).parse(timeString); 178 } catch (ParseException e) { 179 LOG.error("Error parsing timestamp for [" + description + "]", e); 180 throw new CruiseControlException("Cannot parse string for " + description + ":" + timeString); 181 } 182 183 return date; 184 } 185 186 190 public static String formatCVSDate(Date date) { 191 DateFormat format = new SimpleDateFormat (CVS_DATE_PATTERN); 192 format.setTimeZone(UTC); 193 return format.format(date); 194 } 195 196 201 public static Date parseCVSDate(String text) throws ParseException { 202 DateFormat format = new SimpleDateFormat (CVS_DATE_PATTERN); 203 format.setTimeZone(UTC); 204 return format.parse(text); 205 } 206 207 210 public static String getDurationAsString(final long buildLength) { 211 long timeSeconds = buildLength / 1000; 212 long minutes = (timeSeconds / 60); 213 long seconds = timeSeconds - (minutes * 60); 214 return minutes + " minute(s) " + seconds + " second(s)"; 215 } 216 } 217 | Popular Tags |