1 18 package org.apache.tools.ant.util; 19 20 import java.text.ChoiceFormat ; 21 import java.text.DateFormat ; 22 import java.text.MessageFormat ; 23 import java.text.ParseException ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Calendar ; 26 import java.util.Date ; 27 import java.util.Locale ; 28 import java.util.TimeZone ; 29 30 38 public final class DateUtils { 39 40 44 public static final String ISO8601_DATETIME_PATTERN 45 = "yyyy-MM-dd'T'HH:mm:ss"; 46 47 50 public static final String ISO8601_DATE_PATTERN 51 = "yyyy-MM-dd"; 52 53 56 public static final String ISO8601_TIME_PATTERN 57 = "HH:mm:ss"; 58 59 62 public static final DateFormat DATE_HEADER_FORMAT 63 = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss ", Locale.US); 64 65 66 private static final MessageFormat MINUTE_SECONDS 68 = new MessageFormat ("{0}{1}"); 69 70 private static final double[] LIMITS = {0, 1, 2}; 71 72 private static final String [] MINUTES_PART = {"", "1 minute ", "{0,number} minutes "}; 73 74 private static final String [] SECONDS_PART = {"0 seconds", "1 second", "{1,number} seconds"}; 75 76 private static final ChoiceFormat MINUTES_FORMAT = 77 new ChoiceFormat (LIMITS, MINUTES_PART); 78 79 private static final ChoiceFormat SECONDS_FORMAT = 80 new ChoiceFormat (LIMITS, SECONDS_PART); 81 82 static { 83 MINUTE_SECONDS.setFormat(0, MINUTES_FORMAT); 84 MINUTE_SECONDS.setFormat(1, SECONDS_FORMAT); 85 } 86 87 88 private DateUtils() { 89 } 90 91 92 98 public static String format(long date, String pattern) { 99 return format(new Date (date), pattern); 100 } 101 102 103 109 public static String format(Date date, String pattern) { 110 DateFormat df = createDateFormat(pattern); 111 return df.format(date); 112 } 113 114 115 127 public static String formatElapsedTime(long millis) { 128 long seconds = millis / 1000; 129 long minutes = seconds / 60; 130 Object [] args = {new Long (minutes), new Long (seconds % 60)}; 131 return MINUTE_SECONDS.format(args); 132 } 133 134 139 private static DateFormat createDateFormat(String pattern) { 140 SimpleDateFormat sdf = new SimpleDateFormat (pattern); 141 TimeZone gmt = TimeZone.getTimeZone("GMT"); 142 sdf.setTimeZone(gmt); 143 sdf.setLenient(true); 144 return sdf; 145 } 146 147 181 public static int getPhaseOfMoon(Calendar cal) { 182 int dayOfTheYear = cal.get(Calendar.DAY_OF_YEAR); 183 int yearInMetonicCycle = ((cal.get(Calendar.YEAR) - 1900) % 19) + 1; 184 int epact = (11 * yearInMetonicCycle + 18) % 30; 185 if ((epact == 25 && yearInMetonicCycle > 11) || epact == 24) { 186 epact++; 187 } 188 return (((((dayOfTheYear + epact) * 6) + 11) % 177) / 22) & 7; 189 } 190 191 197 public static String getDateForHeader() { 198 Calendar cal = Calendar.getInstance(); 199 TimeZone tz = cal.getTimeZone(); 200 int offset = tz.getOffset(cal.get(Calendar.ERA), 201 cal.get(Calendar.YEAR), 202 cal.get(Calendar.MONTH), 203 cal.get(Calendar.DAY_OF_MONTH), 204 cal.get(Calendar.DAY_OF_WEEK), 205 cal.get(Calendar.MILLISECOND)); 206 StringBuffer tzMarker = new StringBuffer (offset < 0 ? "-" : "+"); 207 offset = Math.abs(offset); 208 int hours = offset / (60 * 60 * 1000); 209 int minutes = offset / (60 * 1000) - 60 * hours; 210 if (hours < 10) { 211 tzMarker.append("0"); 212 } 213 tzMarker.append(hours); 214 if (minutes < 10) { 215 tzMarker.append("0"); 216 } 217 tzMarker.append(minutes); 218 return DATE_HEADER_FORMAT.format(cal.getTime()) + tzMarker.toString(); 219 } 220 221 232 public static Date parseIso8601DateTime(String datestr) 233 throws ParseException { 234 return new SimpleDateFormat (ISO8601_DATETIME_PATTERN).parse(datestr); 235 } 236 237 248 public static Date parseIso8601Date(String datestr) throws ParseException { 249 return new SimpleDateFormat (ISO8601_DATE_PATTERN).parse(datestr); 250 } 251 252 263 public static Date parseIso8601DateTimeOrDate(String datestr) 264 throws ParseException { 265 try { 266 return parseIso8601DateTime(datestr); 267 } catch (ParseException px) { 268 return parseIso8601Date(datestr); 269 } 270 } 271 } 272 | Popular Tags |