1 24 package org.ofbiz.base.util; 25 26 import java.sql.Timestamp ; 27 import java.text.DecimalFormat ; 28 import java.text.SimpleDateFormat ; 29 import java.util.ArrayList ; 30 import java.util.Calendar ; 31 import java.util.Date ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 35 44 public class UtilDateTime { 45 public static final String [] months = { "January", "February", "March", "April", "May", "June", 47 "July", "August", "September", "October", "November", 48 "December" 49 }; 50 51 public static final String [] days = { "Monday", "Tuesday", "Wednesday", 53 "Thursday", "Friday", "Saturday", "Sunday" 54 }; 55 56 public static final String [][] timevals = { 57 {"1000", "millisecond"}, 58 {"60", "second"}, 59 {"60", "minute"}, 60 {"24", "hour"}, 61 {"168", "week"} 62 }; 63 64 public static final DecimalFormat df = new DecimalFormat ("0.00;-0.00"); 65 66 public static double getInterval(Date from, Date thru) { 67 return thru != null ? thru.getTime() - from.getTime() : 0; 68 } 69 70 public static double getInterval(Timestamp from, Timestamp thru) { 71 return thru != null ? thru.getTime() - from.getTime() + (thru.getNanos() - from.getNanos()) / 1000000 : 0; 72 } 73 74 public static String formatInterval(Date from, Date thru, int count, Locale locale) { 75 return formatInterval(getInterval(from, thru), count, locale); 76 } 77 78 public static String formatInterval(Date from, Date thru, Locale locale) { 79 return formatInterval(from, thru, 2, locale); 80 } 81 82 public static String formatInterval(Timestamp from, Timestamp thru, int count, Locale locale) { 83 return formatInterval(getInterval(from, thru), count, locale); 84 } 85 86 public static String formatInterval(Timestamp from, Timestamp thru, Locale locale) { 87 return formatInterval(from, thru, 2, locale); 88 } 89 90 public static String formatInterval(long interval, int count, Locale locale) { 91 return formatInterval((double) interval, count, locale); 92 } 93 94 public static String formatInterval(long interval, Locale locale) { 95 return formatInterval(interval, 2, locale); 96 } 97 98 public static String formatInterval(double interval, Locale locale) { 99 return formatInterval(interval, 2, locale); 100 } 101 102 public static String formatInterval(double interval, int count, Locale locale) { 103 ArrayList parts = new ArrayList (timevals.length); 104 for (int i = 0; i < timevals.length; i++) { 105 int value = Integer.parseInt(timevals[i][0]); 106 double remainder = interval % value; 107 interval = interval / value; 108 parts.add(new Double (remainder)); 109 } 110 111 Map uiDateTimeMap = UtilProperties.getResourceBundleMap("DateTimeLabels", locale); 112 113 StringBuffer sb = new StringBuffer (); 114 for (int i = parts.size() - 1; i >= 0 && count > 0; i--) { 115 if (sb.length() > 0) sb.append(", "); 116 Double D = (Double ) parts.get(i); 117 double d = D.doubleValue(); 118 if (d < 1) continue; 119 count--; 120 sb.append(count == 0 ? df.format(d) : Integer.toString(D.intValue())); 121 sb.append(' '); 122 String label; 123 if (D.intValue() == 1) { 124 label = (String ) uiDateTimeMap.get(timevals[i][1] + ".singular"); 125 } else { 126 label = (String ) uiDateTimeMap.get(timevals[i][1] + ".plural"); 127 } 128 sb.append(label); 129 } 130 return sb.toString(); 131 } 132 133 138 public static java.sql.Timestamp nowTimestamp() { 139 return getTimestamp(System.currentTimeMillis()); 140 } 141 142 public static java.sql.Timestamp getTimestamp(long time) { 143 return new java.sql.Timestamp (time); 144 } 145 146 151 public static String nowDateString() { 152 return nowDateString("yyyyMMddHHmmss"); 153 } 154 155 160 public static String nowDateString(String format) { 161 SimpleDateFormat df = new SimpleDateFormat (format); 162 return df.format(new Date ()); 163 } 164 165 170 public static java.util.Date nowDate() { 171 return new java.util.Date (); 172 } 173 174 public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp) { 175 return getDayStart(stamp, 0); 176 } 177 178 public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp, int daysLater) { 179 Calendar tempCal = Calendar.getInstance(); 180 tempCal.setTime(new java.util.Date (stamp.getTime())); 181 tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); 182 tempCal.add(Calendar.DAY_OF_MONTH, daysLater); 183 java.sql.Timestamp retStamp = new java.sql.Timestamp (tempCal.getTime().getTime()); 184 retStamp.setNanos(0); 185 return retStamp; 186 } 187 188 public static java.sql.Timestamp getNextDayStart(java.sql.Timestamp stamp) { 189 return getDayStart(stamp, 1); 190 } 191 192 public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp) { 193 return getDayEnd(stamp, 0); 194 } 195 196 public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp, int daysLater) { 197 Calendar tempCal = Calendar.getInstance(); 198 tempCal.setTime(new java.util.Date (stamp.getTime())); 199 tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 23, 59, 59); 200 tempCal.add(Calendar.DAY_OF_MONTH, daysLater); 201 java.sql.Timestamp retStamp = new java.sql.Timestamp (tempCal.getTime().getTime()); 202 retStamp.setNanos(999999999); 203 return retStamp; 204 } 205 206 212 public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp) { 213 return getYearStart(stamp, 0, 0, 0); 214 } 215 216 public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, int daysLater) { 217 return getYearStart(stamp, daysLater, 0, 0); 218 } 219 220 public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, int daysLater, int yearsLater) { 221 return getYearStart(stamp, daysLater, 0, yearsLater); 222 } 223 public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, int daysLater, int monthsLater, int yearsLater) { 224 Calendar tempCal = Calendar.getInstance(); 225 tempCal.setTime(new java.util.Date (stamp.getTime())); 226 tempCal.set(tempCal.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0); 227 tempCal.add(Calendar.YEAR, yearsLater); 228 tempCal.add(Calendar.MONTH, monthsLater); 229 tempCal.add(Calendar.DAY_OF_MONTH, daysLater); 230 java.sql.Timestamp retStamp = new java.sql.Timestamp (tempCal.getTime().getTime()); 231 retStamp.setNanos(0); 232 return retStamp; 233 } 234 public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp, Number daysLater, Number monthsLater, Number yearsLater) { 235 return getYearStart(stamp, (daysLater == null ? 0 : daysLater.intValue()), 236 (monthsLater == null ? 0 : monthsLater.intValue()), (yearsLater == null ? 0 : yearsLater.intValue())); 237 } 238 239 245 public static java.sql.Timestamp getMonthStart(java.sql.Timestamp stamp) { 246 return getMonthStart(stamp, 0, 0); 247 } 248 249 public static java.sql.Timestamp getMonthStart(java.sql.Timestamp stamp, int daysLater) { 250 return getMonthStart(stamp, daysLater, 0); 251 } 252 253 public static java.sql.Timestamp getMonthStart(java.sql.Timestamp stamp, int daysLater, int monthsLater) { 254 Calendar tempCal = Calendar.getInstance(); 255 tempCal.setTime(new java.util.Date (stamp.getTime())); 256 tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), 1, 0, 0, 0); 257 tempCal.add(Calendar.DAY_OF_MONTH, daysLater); 258 tempCal.add(Calendar.MONTH, monthsLater); 259 java.sql.Timestamp retStamp = new java.sql.Timestamp (tempCal.getTime().getTime()); 260 retStamp.setNanos(0); 261 return retStamp; 262 } 263 264 270 public static java.sql.Timestamp getWeekStart(java.sql.Timestamp stamp) { 271 return getWeekStart(stamp, 0, 0); 272 } 273 274 public static java.sql.Timestamp getWeekStart(java.sql.Timestamp stamp, int daysLater) { 275 return getWeekStart(stamp, daysLater, 0); 276 } 277 278 public static java.sql.Timestamp getWeekStart(java.sql.Timestamp stamp, int daysLater, int weeksLater) { 279 Calendar tempCal = Calendar.getInstance(); 280 tempCal.setTime(new java.util.Date (stamp.getTime())); 281 tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); 282 tempCal.add(Calendar.DAY_OF_MONTH, daysLater); 283 tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek()); 284 tempCal.add(Calendar.WEEK_OF_MONTH, weeksLater); 285 java.sql.Timestamp retStamp = new java.sql.Timestamp (tempCal.getTime().getTime()); 286 retStamp.setNanos(0); 287 return retStamp; 288 } 289 290 public static java.sql.Timestamp getWeekEnd(java.sql.Timestamp stamp) { 291 Calendar tempCal = Calendar.getInstance(); 292 tempCal.setTime(new java.util.Date (stamp.getTime())); 293 tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); 294 tempCal.add(Calendar.WEEK_OF_MONTH, 1); 295 tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getFirstDayOfWeek()); 296 tempCal.add(Calendar.SECOND, -1); 297 java.sql.Timestamp retStamp = new java.sql.Timestamp (tempCal.getTime().getTime()); 298 retStamp.setNanos(0); 299 return retStamp; 300 } 301 302 public static java.util.Calendar toCalendar(java.sql.Timestamp stamp) { 303 Calendar cal = Calendar.getInstance(); 304 if (stamp != null) { 305 cal.setTimeInMillis(stamp.getTime()); 306 } 307 return cal; 308 } 309 310 316 public static java.sql.Date toSqlDate(String date) { 317 java.util.Date newDate = toDate(date, "00:00:00"); 318 319 if (newDate != null) { 320 return new java.sql.Date (newDate.getTime()); 321 } else { 322 return null; 323 } 324 } 325 326 334 public static java.sql.Date toSqlDate(String monthStr, String dayStr, String yearStr) { 335 java.util.Date newDate = toDate(monthStr, dayStr, yearStr, "0", "0", "0"); 336 337 if (newDate != null) { 338 return new java.sql.Date (newDate.getTime()); 339 } else { 340 return null; 341 } 342 } 343 344 352 public static java.sql.Date toSqlDate(int month, int day, int year) { 353 java.util.Date newDate = toDate(month, day, year, 0, 0, 0); 354 355 if (newDate != null) { 356 return new java.sql.Date (newDate.getTime()); 357 } else { 358 return null; 359 } 360 } 361 362 368 public static java.sql.Time toSqlTime(String time) { 369 java.util.Date newDate = toDate("1/1/1970", time); 370 371 if (newDate != null) { 372 return new java.sql.Time (newDate.getTime()); 373 } else { 374 return null; 375 } 376 } 377 378 386 public static java.sql.Time toSqlTime(String hourStr, String minuteStr, String secondStr) { 387 java.util.Date newDate = toDate("0", "0", "0", hourStr, minuteStr, secondStr); 388 389 if (newDate != null) { 390 return new java.sql.Time (newDate.getTime()); 391 } else { 392 return null; 393 } 394 } 395 396 404 public static java.sql.Time toSqlTime(int hour, int minute, int second) { 405 java.util.Date newDate = toDate(0, 0, 0, hour, minute, second); 406 407 if (newDate != null) { 408 return new java.sql.Time (newDate.getTime()); 409 } else { 410 return null; 411 } 412 } 413 414 420 public static java.sql.Timestamp toTimestamp(String dateTime) { 421 java.util.Date newDate = toDate(dateTime); 422 423 if (newDate != null) { 424 return new java.sql.Timestamp (newDate.getTime()); 425 } else { 426 return null; 427 } 428 } 429 430 437 public static java.sql.Timestamp toTimestamp(String date, String time) { 438 java.util.Date newDate = toDate(date, time); 439 440 if (newDate != null) { 441 return new java.sql.Timestamp (newDate.getTime()); 442 } else { 443 return null; 444 } 445 } 446 447 458 public static java.sql.Timestamp toTimestamp(String monthStr, String dayStr, String yearStr, String hourStr, 459 String minuteStr, String secondStr) { 460 java.util.Date newDate = toDate(monthStr, dayStr, yearStr, hourStr, minuteStr, secondStr); 461 462 if (newDate != null) { 463 return new java.sql.Timestamp (newDate.getTime()); 464 } else { 465 return null; 466 } 467 } 468 469 480 public static java.sql.Timestamp toTimestamp(int month, int day, int year, int hour, int minute, int second) { 481 java.util.Date newDate = toDate(month, day, year, hour, minute, second); 482 483 if (newDate != null) { 484 return new java.sql.Timestamp (newDate.getTime()); 485 } else { 486 return null; 487 } 488 } 489 490 public static java.sql.Timestamp toTimestamp(Date date) { 491 if (date == null) return null; 492 return new Timestamp (date.getTime()); 493 } 494 495 501 public static java.util.Date toDate(String dateTime) { 502 if (dateTime == null) { 503 return null; 504 } 505 String date = dateTime.substring(0, dateTime.indexOf(" ")); 507 String time = dateTime.substring(dateTime.indexOf(" ") + 1); 508 509 return toDate(date, time); 510 } 511 512 519 public static java.util.Date toDate(String date, String time) { 520 if (date == null || time == null) return null; 521 String month; 522 String day; 523 String year; 524 String hour; 525 String minute; 526 String second; 527 528 int dateSlash1 = date.indexOf("/"); 529 int dateSlash2 = date.lastIndexOf("/"); 530 531 if (dateSlash1 <= 0 || dateSlash1 == dateSlash2) return null; 532 int timeColon1 = time.indexOf(":"); 533 int timeColon2 = time.lastIndexOf(":"); 534 535 if (timeColon1 <= 0) return null; 536 month = date.substring(0, dateSlash1); 537 day = date.substring(dateSlash1 + 1, dateSlash2); 538 year = date.substring(dateSlash2 + 1); 539 hour = time.substring(0, timeColon1); 540 541 if (timeColon1 == timeColon2) { 542 minute = time.substring(timeColon1 + 1); 543 second = "0"; 544 } else { 545 minute = time.substring(timeColon1 + 1, timeColon2); 546 second = time.substring(timeColon2 + 1); 547 } 548 549 return toDate(month, day, year, hour, minute, second); 550 } 551 552 563 public static java.util.Date toDate(String monthStr, String dayStr, String yearStr, String hourStr, 564 String minuteStr, String secondStr) { 565 int month, day, year, hour, minute, second; 566 567 try { 568 month = Integer.parseInt(monthStr); 569 day = Integer.parseInt(dayStr); 570 year = Integer.parseInt(yearStr); 571 hour = Integer.parseInt(hourStr); 572 minute = Integer.parseInt(minuteStr); 573 second = Integer.parseInt(secondStr); 574 } catch (Exception e) { 575 return null; 576 } 577 return toDate(month, day, year, hour, minute, second); 578 } 579 580 591 public static java.util.Date toDate(int month, int day, int year, int hour, int minute, int second) { 592 Calendar calendar = Calendar.getInstance(); 593 594 try { 595 calendar.set(year, month - 1, day, hour, minute, second); 596 } catch (Exception e) { 597 return null; 598 } 599 return new java.util.Date (calendar.getTime().getTime()); 600 } 601 602 608 public static String toDateString(java.util.Date date, String format) { 609 if (date == null) return ""; 610 SimpleDateFormat dateFormat = null; 611 if (format != null) { 612 dateFormat = new SimpleDateFormat (format); 613 } else { 614 dateFormat = new SimpleDateFormat (); 615 } 616 617 Calendar calendar = Calendar.getInstance(); 618 619 calendar.setTime(date); 620 return dateFormat.format(date); 621 } 622 628 public static String toDateString(java.util.Date date) { 629 return toDateString(date, "MM/dd/yyyy"); 630 } 631 632 638 public static String toTimeString(java.util.Date date) { 639 if (date == null) return ""; 640 Calendar calendar = Calendar.getInstance(); 641 642 calendar.setTime(date); 643 return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND))); 644 } 645 646 654 public static String toTimeString(int hour, int minute, int second) { 655 String hourStr; 656 String minuteStr; 657 String secondStr; 658 659 if (hour < 10) { 660 hourStr = "0" + hour; 661 } else { 662 hourStr = "" + hour; 663 } 664 if (minute < 10) { 665 minuteStr = "0" + minute; 666 } else { 667 minuteStr = "" + minute; 668 } 669 if (second < 10) { 670 secondStr = "0" + second; 671 } else { 672 secondStr = "" + second; 673 } 674 if (second == 0) { 675 return hourStr + ":" + minuteStr; 676 } else { 677 return hourStr + ":" + minuteStr + ":" + secondStr; 678 } 679 } 680 681 687 public static String toDateTimeString(java.util.Date date) { 688 if (date == null) return ""; 689 String dateString = toDateString(date); 690 String timeString = toTimeString(date); 691 692 if (dateString != null && timeString != null) { 693 return dateString + " " + timeString; 694 } else { 695 return ""; 696 } 697 } 698 699 704 public static java.sql.Timestamp monthBegin() { 705 Calendar mth = Calendar.getInstance(); 706 707 mth.set(Calendar.DAY_OF_MONTH, 1); 708 mth.set(Calendar.HOUR_OF_DAY, 0); 709 mth.set(Calendar.MINUTE, 0); 710 mth.set(Calendar.SECOND, 0); 711 mth.set(Calendar.MILLISECOND, 0); 712 mth.set(Calendar.AM_PM, Calendar.AM); 713 return new java.sql.Timestamp (mth.getTime().getTime()); 714 } 715 716 722 public static int weekNumber(Timestamp input) { 723 Timestamp yearStart = UtilDateTime.getYearStart(input); 724 Timestamp weekStart = UtilDateTime.getWeekStart(yearStart); 725 726 int days = 0; 727 for (days = 0; UtilDateTime.getDayStart(weekStart, days).compareTo(yearStart) == 0; days++) ; 728 729 Timestamp week1Start = weekStart; 731 if (days < 4) 732 week1Start = UtilDateTime.getWeekStart(weekStart, 7); 733 734 int weeks = 0; 735 for (weeks = 0; UtilDateTime.getDayStart(week1Start, weeks * 7).compareTo(input) < 0; weeks++) ; 736 737 return ++weeks; } 739 } 740 741 | Popular Tags |