1 package org.roller.util; 2 3 import java.text.ParseException ; 4 import java.text.SimpleDateFormat ; 5 import java.util.Calendar ; 6 import java.util.Date ; 7 import java.util.Locale ; 8 9 14 public abstract class DateUtil extends Object 15 { 16 public static final long millisInDay = 86400000; 17 18 private static SimpleDateFormat [] mDateFormats = loadDateFormats(); 20 21 private static final SimpleDateFormat mFormat8chars = 22 new SimpleDateFormat ("yyyyMMdd"); 23 24 private static final SimpleDateFormat mFormatIso8601Day = 25 new SimpleDateFormat ("yyyy-MM-dd"); 26 27 private static final SimpleDateFormat mFormatIso8601 = 28 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssZ"); 29 30 private static final SimpleDateFormat mFormatRfc822 = 33 new SimpleDateFormat ("EEE, d MMM yyyy HH:mm:ss Z", Locale.US); 34 35 private static SimpleDateFormat [] loadDateFormats() 36 { 37 SimpleDateFormat [] temp = { 38 new SimpleDateFormat ("EEE MMM d HH:mm:ss z yyyy"), new SimpleDateFormat ("M/d/yy hh:mm:ss"), 41 new SimpleDateFormat ("M/d/yyyy hh:mm:ss"), 42 new SimpleDateFormat ("M/d/yy hh:mm a"), 43 new SimpleDateFormat ("M/d/yyyy hh:mm a"), 44 new SimpleDateFormat ("M/d/yy HH:mm"), 45 new SimpleDateFormat ("M/d/yyyy HH:mm"), 46 new SimpleDateFormat ("dd.MM.yyyy HH:mm:ss"), 47 new SimpleDateFormat ("yy-MM-dd HH:mm:ss.SSS"), 48 new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS"), new SimpleDateFormat ("M-d-yy HH:mm"), 50 new SimpleDateFormat ("M-d-yyyy HH:mm"), 51 new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss.SSS"), 52 new SimpleDateFormat ("M/d/yy"), 53 new SimpleDateFormat ("M/d/yyyy"), 54 new SimpleDateFormat ("M-d-yy"), 55 new SimpleDateFormat ("M-d-yyyy"), 56 new SimpleDateFormat ("MMMM d, yyyyy"), 57 new SimpleDateFormat ("MMM d, yyyyy") 58 }; 59 60 return temp; 61 } 62 66 private static SimpleDateFormat [] getFormats() 67 { 68 return mDateFormats; 69 } 70 71 77 public static Date getEndOfDay(Date day) 78 { 79 return getEndOfDay(day,Calendar.getInstance()); 80 } 81 public static Date getEndOfDay(Date day,Calendar cal) 82 { 83 if (day == null) day = new Date (); 84 cal.setTime(day); 85 cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY)); 86 cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE)); 87 cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND)); 88 cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND)); 89 return cal.getTime(); 90 } 91 92 98 public static Date getStartOfDay(Date day) 99 { 100 return getStartOfDay(day, Calendar.getInstance()); 101 } 102 107 public static Date getStartOfDay(Date day, Calendar cal) 108 { 109 if (day == null) day = new Date (); 110 cal.setTime(day); 111 cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY)); 112 cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE)); 113 cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND)); 114 cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND)); 115 return cal.getTime(); 116 } 117 118 123 public static Date getNoonOfDay(Date day, Calendar cal) 124 { 125 if (day == null) day = new Date (); 126 cal.setTime(day); 127 cal.set(Calendar.HOUR_OF_DAY, 12); 128 cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE)); 129 cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND)); 130 cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND)); 131 return cal.getTime(); 132 } 133 134 public static Date parseFromFormats(String aValue) 136 { 137 if (StringUtils.isEmpty(aValue)) return null; 138 139 SimpleDateFormat formats[] = DateUtil.getFormats(); 141 if (formats == null) return null; 142 143 Date myDate = null; 145 for (int i = 0; i <formats.length; i++) 146 { 147 try 148 { 149 myDate = DateUtil.parse(aValue, formats[i]); 150 return myDate; 152 } 153 catch (Exception e) 154 { 155 } 158 } 159 return null; 161 } 162 163 public static java.sql.Timestamp parseTimestampFromFormats(String aValue) 165 { 166 if (StringUtils.isEmpty(aValue)) return null; 167 168 Date myDate = DateUtil.parseFromFormats(aValue); 170 if (myDate != null) return new java.sql.Timestamp (myDate.getTime()); 171 return null; 172 } 173 177 public static java.sql.Timestamp now() 178 { 179 return new java.sql.Timestamp (new java.util.Date ().getTime()); 180 } 181 182 188 public static String format(Date aDate, SimpleDateFormat aFormat) 189 { 190 if (aDate == null || aFormat == null ) { return ""; } 191 synchronized (aFormat) 192 { 193 return aFormat.format(aDate); 194 } 195 } 196 197 202 public static String formatDateString(String aString, SimpleDateFormat aFormat) 203 { 204 if (StringUtils.isEmpty(aString) || aFormat == null) return ""; 205 try 206 { 207 java.sql.Timestamp aDate = parseTimestampFromFormats(aString); 208 if (aDate != null) 209 { 210 return DateUtil.format(aDate, aFormat); 211 } 212 } 213 catch (Exception e) 214 { 215 } 217 return ""; 218 } 219 220 225 public static Date parse(String aValue, SimpleDateFormat aFormat) throws ParseException 226 { 227 if (StringUtils.isEmpty(aValue) || aFormat == null) 228 { 229 return null; 230 } 231 232 return aFormat.parse(aValue); 233 } 234 235 240 public static boolean isValidDateRange(Date startDate, Date endDate) 241 { 242 return isValidDateRange(startDate, endDate, true); 243 } 244 245 251 public static boolean isValidDateRange(Date startDate, Date endDate, boolean equalOK) 252 { 253 if (startDate == null || endDate == null) { return false; } 255 256 if (equalOK) 257 { 258 if (startDate.equals(endDate)) { return true; } 260 } 261 262 if (endDate.after(startDate)) { return true; } 264 265 return false; 266 } 267 268 public static java.text.SimpleDateFormat defaultTimestampFormat() 271 { 272 return new java.text.SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS"); 273 } 274 275 public static java.text.SimpleDateFormat get8charDateFormat() 278 { 279 return DateUtil.mFormat8chars; 280 } 281 282 public static java.text.SimpleDateFormat defaultDateFormat() 285 { 286 return DateUtil.friendlyDateFormat(true); 287 } 288 289 public static String defaultTimestamp(Date date) 292 { 293 return DateUtil.format(date, DateUtil.defaultTimestampFormat()); 294 } 295 296 public static String defaultDate(Date date) 299 { 300 return DateUtil.format(date, DateUtil.defaultDateFormat()); 301 } 302 303 public static java.text.SimpleDateFormat friendlyTimestampFormat() 306 { 307 return new java.text.SimpleDateFormat ("dd.MM.yyyy HH:mm:ss"); 308 } 309 310 public static String friendlyTimestamp(Date date) 313 { 314 return DateUtil.format(date, DateUtil.friendlyTimestampFormat()); 315 } 316 317 public static String format8chars(Date date) 320 { 321 return DateUtil.format(date, mFormat8chars); 322 } 323 324 public static String formatIso8601Day(Date date) 327 { 328 return DateUtil.format(date, mFormatIso8601Day); 329 } 330 331 public static String formatRfc822(Date date) 333 { 334 return DateUtil.format(date,mFormatRfc822); 335 } 336 337 public static String formatIso8601(Date date) 340 { 341 if (date == null) return ""; 342 343 346 String str = DateUtil.format(date,mFormatIso8601); 347 StringBuffer sb = new StringBuffer (); 348 sb.append( str.substring(0,str.length()-2) ); 349 sb.append( ":" ); 350 sb.append( str.substring(str.length()-2) ); 351 return sb.toString(); 352 } 353 354 public static java.text.SimpleDateFormat minimalDateFormat() 357 { 358 return DateUtil.friendlyDateFormat(true); 359 } 360 361 public static String minimalDate(Date date) 364 { 365 return DateUtil.format(date, DateUtil.minimalDateFormat()); 366 } 367 368 public static java.text.SimpleDateFormat fullDateFormat() 372 { 373 return DateUtil.friendlyDateFormat(false); 374 } 375 376 public static String fullDate(Date date) 378 { 379 return DateUtil.format(date, DateUtil.fullDateFormat()); 380 } 381 382 386 public static java.text.SimpleDateFormat friendlyDateFormat(boolean minimalFormat) 387 { 388 if (minimalFormat) 389 { 390 return new java.text.SimpleDateFormat ("d.M.yy"); 391 } 392 393 return new java.text.SimpleDateFormat ("dd.MM.yyyy"); 394 } 395 396 400 public static String friendlyDate(Date date, boolean minimalFormat) 401 { 402 return DateUtil.format(date, DateUtil.friendlyDateFormat(minimalFormat)); 403 } 404 405 public static String friendlyDate(Date date) 408 { 409 return DateUtil.format(date, DateUtil.friendlyDateFormat(true)); 410 } 411 412 public static Date parseIso8601(String value) throws Exception 413 { 414 return ISO8601DateParser.parse(value); 415 } 416 } 417 | Popular Tags |