1 18 19 package org.apache.roller.util; 20 21 import java.text.ParseException ; 22 import java.text.ParsePosition ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Calendar ; 25 import java.util.Date ; 26 import java.util.Locale ; 27 import java.util.TimeZone ; 28 import org.apache.commons.lang.StringUtils; 29 30 31 34 public abstract class DateUtil { 35 36 public static final long millisInDay = 86400000; 37 38 private static final String formatDefaultDate = "dd.MM.yyyy"; 40 private static final String formatDefaultDateMinimal = "d.M.yy"; 41 private static final String formatDefaultTimestamp = "yyyy-MM-dd HH:mm:ss.SSS"; 42 43 private static final String formatFriendlyTimestamp = "dd.MM.yyyy HH:mm:ss"; 44 45 private static final String format6chars = "yyyyMM"; 46 private static final String format8chars = "yyyyMMdd"; 47 48 private static final String formatIso8601 = "yyyy-MM-dd'T'HH:mm:ssZ"; 49 private static final String formatIso8601Day = "yyyy-MM-dd"; 50 51 private static final String formatRfc822 = "EEE, d MMM yyyy HH:mm:ss Z"; 52 53 54 59 public static Date getEndOfDay(Date day) { 60 return getEndOfDay(day,Calendar.getInstance()); 61 } 62 63 64 public static Date getEndOfDay(Date day,Calendar cal) { 65 if (day == null) day = new Date (); 66 cal.setTime(day); 67 cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY)); 68 cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE)); 69 cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND)); 70 cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND)); 71 return cal.getTime(); 72 } 73 74 75 80 public static Date getStartOfMonth(Date day) { 81 return getStartOfMonth(day, Calendar.getInstance()); 82 } 83 84 85 public static Date getStartOfMonth(Date day, Calendar cal) { 86 if (day == null) day = new Date (); 87 cal.setTime(day); 88 89 cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY)); 91 cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE)); 92 cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND)); 93 cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND)); 94 95 cal.set(Calendar.DAY_OF_MONTH, 1); 97 98 return cal.getTime(); 99 } 100 101 102 107 public static Date getEndOfMonth(Date day) { 108 return getEndOfMonth(day, Calendar.getInstance()); 109 } 110 111 112 public static Date getEndOfMonth(Date day,Calendar cal) { 113 if (day == null) day = new Date (); 114 cal.setTime(day); 115 116 cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY)); 118 cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE)); 119 cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND)); 120 cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND)); 121 122 cal.set(Calendar.DAY_OF_MONTH, 1); 124 125 cal.add(Calendar.MONTH, 1); 127 128 cal.add(Calendar.DAY_OF_MONTH, -1); 130 131 return cal.getTime(); 132 } 133 134 135 140 public static Date getStartOfDay(Date day) { 141 return getStartOfDay(day, Calendar.getInstance()); 142 } 143 144 145 150 public static Date getStartOfDay(Date day, Calendar cal) { 151 if (day == null) day = new Date (); 152 cal.setTime(day); 153 cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY)); 154 cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE)); 155 cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND)); 156 cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND)); 157 return cal.getTime(); 158 } 159 160 161 166 public static Date getNoonOfDay(Date day, Calendar cal) { 167 if (day == null) day = new Date (); 168 cal.setTime(day); 169 cal.set(Calendar.HOUR_OF_DAY, 12); 170 cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE)); 171 cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND)); 172 cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND)); 173 return cal.getTime(); 174 } 175 176 177 180 public static java.sql.Timestamp now() { 181 return new java.sql.Timestamp (new java.util.Date ().getTime()); 182 } 183 184 185 190 public static String format(Date aDate, SimpleDateFormat aFormat) { 191 if (aDate == null || aFormat == null ) { return ""; } 192 synchronized (aFormat) { 193 return aFormat.format(aDate); 194 } 195 } 196 197 198 202 public static Date parse(String aValue, SimpleDateFormat aFormat) throws ParseException { 203 if (StringUtils.isEmpty(aValue) || aFormat == null) { 204 return null; 205 } 206 synchronized(aFormat) { 207 return aFormat.parse(aValue); 208 } 209 } 210 211 212 216 public static boolean isValidDateRange(Date startDate, Date endDate) { 217 return isValidDateRange(startDate, endDate, true); 218 } 219 220 221 226 public static boolean isValidDateRange(Date startDate, Date endDate, boolean equalOK) { 227 if (startDate == null || endDate == null) { return false; } 229 230 if (equalOK) { 231 if (startDate.equals(endDate)) { return true; } 233 } 234 235 if (endDate.after(startDate)) { return true; } 237 238 return false; 239 } 240 241 242 public static SimpleDateFormat defaultDateFormat() { 244 return DateUtil.friendlyDateFormat(true); 245 } 246 247 248 public static java.text.SimpleDateFormat minimalDateFormat() { 250 return friendlyDateFormat(true); 251 } 252 253 254 public static SimpleDateFormat fullDateFormat() { 257 return friendlyDateFormat(false); 258 } 259 260 261 265 public static SimpleDateFormat friendlyDateFormat(boolean minimalFormat) { 266 if (minimalFormat) { 267 return new SimpleDateFormat (formatDefaultDateMinimal); 268 } 269 270 return new SimpleDateFormat (formatDefaultDate); 271 } 272 273 274 public static SimpleDateFormat defaultTimestampFormat() { 276 return new SimpleDateFormat (formatDefaultTimestamp); 277 } 278 279 280 public static SimpleDateFormat friendlyTimestampFormat() { 282 return new SimpleDateFormat (formatFriendlyTimestamp); 283 } 284 285 286 public static SimpleDateFormat get8charDateFormat() { 288 return new SimpleDateFormat (format8chars); 289 } 290 291 292 public static SimpleDateFormat get6charDateFormat() { 294 return new SimpleDateFormat (format6chars); 295 } 296 297 298 public static SimpleDateFormat getIso8601DateFormat() { 300 return new SimpleDateFormat (formatIso8601); 301 } 302 303 304 public static SimpleDateFormat getIso8601DayDateFormat() { 306 return new SimpleDateFormat (formatIso8601Day); 307 } 308 309 310 public static SimpleDateFormat getRfc822DateFormat() { 312 return new SimpleDateFormat (formatRfc822, Locale.US); 315 } 316 317 318 public static String defaultDate(Date date) { 320 return format(date, defaultDateFormat()); 321 } 322 323 324 public static String minimalDate(Date date) { 326 return format(date, DateUtil.minimalDateFormat()); 327 } 328 329 330 public static String fullDate(Date date) { 331 return format(date, DateUtil.fullDateFormat()); 332 } 333 334 335 338 public static String friendlyDate(Date date, boolean minimalFormat) { 339 return format(date, friendlyDateFormat(minimalFormat)); 340 } 341 342 343 public static String friendlyDate(Date date) { 345 return format(date, friendlyDateFormat(true)); 346 } 347 348 349 public static String defaultTimestamp(Date date) { 351 return format(date, defaultTimestampFormat()); 352 } 353 354 355 public static String friendlyTimestamp(Date date) { 357 return format(date, friendlyTimestampFormat()); 358 } 359 360 361 public static String format8chars(Date date) { 363 return format(date, get8charDateFormat()); 364 } 365 366 367 public static String format6chars(Date date) { 369 return format(date, get6charDateFormat()); 370 } 371 372 373 public static String formatIso8601Day(Date date) { 375 return format(date, getIso8601DayDateFormat()); 376 } 377 378 379 public static String formatRfc822(Date date) { 380 return format(date, getRfc822DateFormat()); 381 } 382 383 384 public static String formatIso8601(Date date) { 386 if (date == null) return ""; 387 388 391 String str = format(date, getIso8601DateFormat()); 392 StringBuffer sb = new StringBuffer (); 393 sb.append( str.substring(0,str.length()-2) ); 394 sb.append( ":" ); 395 sb.append( str.substring(str.length()-2) ); 396 return sb.toString(); 397 } 398 399 400 public static Date parseIso8601(String value) throws Exception { 401 return ISO8601DateParser.parse(value); 402 } 403 404 405 408 public static Date parseWeblogURLDateString(String dateString, TimeZone tz, Locale locale) { 409 410 Date ret = new Date (); 411 SimpleDateFormat char8DateFormat = DateUtil.get8charDateFormat(); 412 SimpleDateFormat char6DateFormat = DateUtil.get6charDateFormat(); 413 414 if (dateString != null 415 && dateString.length()==8 416 && StringUtils.isNumeric(dateString) ) { 417 ParsePosition pos = new ParsePosition (0); 418 ret = char8DateFormat.parse(dateString, pos); 419 420 Date today = null; 422 Calendar todayCal = Calendar.getInstance(); 423 todayCal = Calendar.getInstance(tz, locale); 424 todayCal.setTime(new Date ()); 425 today = todayCal.getTime(); 426 if(ret.after(today)) { 427 ret = today; 428 } 429 430 } else if(dateString != null 431 && dateString.length()==6 432 && StringUtils.isNumeric(dateString)) { 433 ParsePosition pos = new ParsePosition (0); 434 ret = char6DateFormat.parse(dateString, pos); 435 436 Calendar todayCal = Calendar.getInstance(); 438 todayCal = Calendar.getInstance(tz, locale); 439 todayCal.setTime(new Date ()); 440 Date today = todayCal.getTime(); 441 if(ret.after(today)) { 442 ret = today; 443 } 444 } 445 446 return ret; 447 } 448 449 } 450 | Popular Tags |