1 64 65 package com.jcorporate.expresso.core.misc; 66 67 import com.jcorporate.expresso.core.db.DBException; 68 import com.jcorporate.expresso.core.i18n.Messages; 69 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 70 import org.apache.log4j.Logger; 71 72 import java.lang.ref.SoftReference ; 73 import java.text.DateFormat ; 74 import java.text.SimpleDateFormat ; 75 import java.util.Calendar ; 76 import java.util.Date ; 77 import java.util.GregorianCalendar ; 78 import java.util.HashMap ; 79 import java.util.Locale ; 80 import java.util.Map ; 81 82 90 public class DateTime { 91 92 private static final Logger log = Logger.getLogger(DateTime.class); 93 94 97 public DateTime() { 98 super(); 99 } 100 101 106 public static String getDateString() { 107 Calendar rightNow = Calendar.getInstance(); 108 109 return ("" + (rightNow.get(Calendar.MONTH) + 1) + "/" + 110 rightNow.get(Calendar.DAY_OF_MONTH) + "/" + 111 rightNow.get(Calendar.YEAR)).trim(); 112 } 113 114 115 121 public static String getDateTimeForDB() throws com.jcorporate.expresso.core.db.DBException { 122 return getDateTimeForDB(new Date (), "default"); 123 } 124 125 132 public static String getDateTimeForDB(String dbContext) throws DBException { 133 return getDateTimeForDB(new Date (), dbContext); 134 } 135 136 149 public static String getDateTimeForDB(int year, int month, 150 int day, int hour, 151 int min, int sec, String context) throws com.jcorporate.expresso.core.db.DBException { 152 Calendar cal = new GregorianCalendar (year, month, day, hour, min, sec); 153 154 return getDateTimeForDB(cal.getTime(), context); 155 } 156 157 164 public static String getDateTimeForDB(Date date, String context) throws com.jcorporate.expresso.core.db.DBException { 165 if (date == null) { 166 return null; 167 } 168 169 String convertFormat = null; 170 171 try { 173 ConfigJdbc myConfig = ConfigManager.getJdbcRequired(context); 174 175 convertFormat = StringUtil.notNull(myConfig.getDateTimeSelectFormat()); 176 } catch (ConfigurationException ce) { 177 throw new com.jcorporate.expresso.core.db.DBException(ce); 178 } 179 180 181 SimpleDateFormat formatter = null; 182 if (convertFormat.equals("")) { 183 formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss "); 185 } else { 186 formatter = new SimpleDateFormat (convertFormat); 187 } 188 189 return formatter.format(date).trim(); 190 191 } 192 193 206 public static String getDateTimeForDB(int year, int month, 207 int day, int hour, 208 int min, int sec) throws com.jcorporate.expresso.core.db.DBException { 209 Calendar cal = new GregorianCalendar (year, month, day, hour, min, sec); 210 211 return getDateTimeForDB(cal.getTime()).trim(); 212 } 213 214 222 public static String getDateTimeForDB(Date date) throws com.jcorporate.expresso.core.db.DBException { 223 return getDateTimeForDB(date, "default"); 224 } 225 226 232 public static String getDateTimeString() { 233 234 241 242 SimpleDateFormat formatter = new SimpleDateFormat ("E',' MMM d yyyy 'at' hh:mm:ss a"); 244 Date currentTime_1 = new Date (); 245 String dateString = formatter.format(currentTime_1); 246 247 return dateString.trim(); 248 } 249 250 257 public static String getDateTimeString(Date date) { 258 if (date == null) { 259 return null; 260 } 261 SimpleDateFormat formatter = new SimpleDateFormat ("E',' MMM d yyyy 'at' hh:mm:ss a"); 263 String dateString = formatter.format(date); 264 265 return dateString.trim(); 266 } 267 268 275 public static String getTimeForDB() throws com.jcorporate.expresso.core.db.DBException { 276 return getTimeForDB(new Date (), "default"); 277 } 278 279 287 public static String getTimeForDB(int hour, int min, int sec) throws com.jcorporate.expresso.core.db.DBException { 288 Calendar cal = new GregorianCalendar (); 289 cal.set(Calendar.HOUR_OF_DAY, hour); 290 cal.set(Calendar.MINUTE, min); 291 cal.set(Calendar.SECOND, sec); 292 293 return getTimeForDB(cal.getTime(), "default"); 294 } 295 296 303 public static String getTimeForDB(Date date) throws com.jcorporate.expresso.core.db.DBException { 304 return getTimeForDB(date, "default"); 305 } 306 307 314 public static String getTimeForDB(Date date, String context) throws com.jcorporate.expresso.core.db.DBException { 315 if (date == null) { 316 return null; 317 } 318 319 StringUtil.assertNotBlank(context, 320 "DateTime.getTimeForDB(): parameter 'context' may not be mull"); 321 322 String convertFormat = null; 323 324 try { 326 ConfigJdbc myConfig = ConfigManager.getJdbcRequired(context); 327 328 convertFormat = StringUtil.notNull(myConfig.getTimeSelectFormat()); 329 } catch (ConfigurationException ce) { 330 throw new com.jcorporate.expresso.core.db.DBException(ce); 331 } 332 333 334 SimpleDateFormat formatter = null; 335 if (convertFormat.equals("")) { 336 formatter = new SimpleDateFormat ("HH:mm:ss"); 338 } else { 339 formatter = new SimpleDateFormat (convertFormat); 340 } 341 342 return formatter.format(date).trim(); 343 } 344 345 352 public static String getDateForDB() throws com.jcorporate.expresso.core.db.DBException { 353 return getDateForDB(new Date (), "default"); 354 } 355 356 365 public static String getDateForDB(int year, int mon, int day) throws com.jcorporate.expresso.core.db.DBException { 366 Calendar cal = new GregorianCalendar (); 367 cal.set(Calendar.YEAR, year); 368 cal.set(Calendar.MONTH, mon); 369 cal.set(Calendar.DAY_OF_MONTH, day); 370 371 return getDateForDB(cal.getTime(), "default"); 372 } 373 374 381 public static String getDateForDB(Date date) throws com.jcorporate.expresso.core.db.DBException { 382 return getDateForDB(date, "default"); 383 } 384 385 393 public static String getDateForDB(Date date, String context) throws DBException { 394 395 if (date == null) { 396 return null; 397 } 398 399 StringUtil.assertNotBlank(context, "DateTime.getDateForDB(): parameter context may not be mull"); 400 401 String convertFormat = null; 402 403 try { 405 ConfigJdbc myConfig = ConfigManager.getJdbcRequired(context); 406 407 convertFormat = StringUtil.notNull(myConfig.getDateSelectFormat()); 408 } catch (ConfigurationException ce) { 409 throw new com.jcorporate.expresso.core.db.DBException(ce); 410 } 411 412 413 SimpleDateFormat formatter = null; 414 if (convertFormat.equals("")) { 415 formatter = new SimpleDateFormat ("yyyy-MM-dd"); 417 } else { 418 formatter = new SimpleDateFormat (convertFormat); 419 } 420 421 return formatter.format(date).trim(); 422 } 423 424 425 428 transient private static SoftReference sDateFormatStrings = new SoftReference (new HashMap ()); 429 430 449 public static String getDateFormatString(boolean dateOnly, Locale locale) { 450 DateTime.DateFormatCacheItem item = null; 451 String pattern = ""; 452 String datePattern = ""; 453 String dateTimePattern = ""; 454 455 synchronized (sDateFormatStrings) { 456 Map dateFormatStrings = (Map ) sDateFormatStrings.get(); 457 if (dateFormatStrings == null) { 458 dateFormatStrings = new HashMap (); 459 sDateFormatStrings = new SoftReference (dateFormatStrings); 460 } 461 462 item = (DateTime.DateFormatCacheItem) dateFormatStrings.get(DateFormatCacheItem.deriveKey(locale)); 463 464 if (item == null) { 465 DateFormat dateFormat; 466 DateFormat dateTimeFormat; 467 dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); 468 dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale); 469 470 if (dateFormat instanceof SimpleDateFormat ) { 471 SimpleDateFormat simpleDateFormat = (SimpleDateFormat ) dateFormat; 472 datePattern = convertDateFormatStringToLocalized(locale, simpleDateFormat.toPattern()); 473 } 474 if (dateTimeFormat instanceof SimpleDateFormat ) { 475 SimpleDateFormat simpleDateFormat = (SimpleDateFormat ) dateTimeFormat; 476 dateTimePattern = convertDateFormatStringToLocalized(locale, simpleDateFormat.toPattern()); 477 } 478 479 item = new DateFormatCacheItem(locale, datePattern, dateTimePattern); 480 dateFormatStrings.put(item.key, item); 481 } else { 482 datePattern = item.dateFormat; 483 dateTimePattern = item.dateTimeFormat; 484 } 486 } 487 488 if (dateOnly) { 489 pattern = datePattern; 490 } else { 491 pattern = dateTimePattern; 492 } 493 494 return pattern; 495 } 496 497 505 private static String convertDateFormatStringToLocalized(Locale locale, String p) { 506 try { 507 FastStringBuffer fsb = new FastStringBuffer(16); 508 int n = p.length(); 509 for (int i = 0; i < n; i++) { 510 char c = p.charAt(i); 511 switch (c) { 512 case 'M': 513 case 'd': 514 case 'y': 515 case 'h': 516 case 'H': 517 case 'k': 518 case 'K': 519 case 'm': 520 case 's': 521 { 522 String s; 523 try { 524 s = Messages.getString(locale, "datetime.format." + c + c); 525 fsb.append(s); 526 } catch (Exception e) { 527 fsb.append(c).append(c); 528 } 529 while (i < n - 1 && c == p.charAt(i + 1)) { 530 i++; 531 } 532 } 533 break; 534 default: 535 fsb.append(c); 536 break; 537 } 538 } 539 return fsb.toString(); 540 } catch (Exception e) { 541 log.warn("Exception parsing date format string to localized string", e); 542 } 543 return ""; 544 } 545 546 550 static class DateFormatCacheItem { 551 public String key; 552 public String dateFormat; 553 public String dateTimeFormat; 554 555 DateFormatCacheItem(Locale locale, String dateFormat, String dateTimeFormat) { 556 key = deriveKey(locale); 557 this.dateFormat = dateFormat; 558 this.dateTimeFormat = dateTimeFormat; 559 } 560 561 public String getKey() { 562 return key; 563 } 564 565 static String deriveKey(Locale locale) { 566 return locale.getCountry() + "-" + locale.getLanguage(); 567 } 568 } 569 570 } 571 | Popular Tags |