1 10 package org.mmbase.util; 11 12 import java.text.*; 13 import java.text.DateFormat ; 14 import java.text.SimpleDateFormat ; 15 import java.util.*; 16 import java.util.Calendar ; 17 import java.util.Date ; 18 import java.util.GregorianCalendar ; 19 import java.util.SimpleTimeZone ; 20 import java.util.StringTokenizer ; 21 import java.util.TimeZone ; 22 23 import org.mmbase.util.logging.Logger; 24 import org.mmbase.util.logging.Logging; 25 26 42 public class DateSupport { 43 44 private static Logger log = Logging.getLoggerInstance(DateSupport.class); 46 47 50 static int offset = 0; 51 55 static boolean dooffset = true; 56 57 64 static public int daysInMonth(int year, int month) { 65 int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 66 int days = months[month]; 67 year = (year < 90) ? year + 2000 : year + 1900; 68 69 if (month == 1) { 71 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) 72 days = 29; 73 } 74 return days; 75 } 76 77 86 static public int secondInYear(Date d) { 87 Calendar c = Calendar.getInstance(); 88 c.setTime(d); 89 return c.get(Calendar.DAY_OF_YEAR) * (60 * 60 * 24) + c.get(Calendar.HOUR_OF_DAY) * (24 * 60) + c.get(Calendar.SECOND); 90 } 91 92 101 static public int dayInYear(Date d) { 102 Calendar c = Calendar.getInstance(); 103 c.setTime(d); 104 return c.get(Calendar.DAY_OF_YEAR); 105 } 106 107 116 static public int weekInYear(Date d) { 117 Calendar c = Calendar.getInstance(); 118 c.setTime(d); 119 return c.get(Calendar.WEEK_OF_YEAR); 120 } 121 122 129 static public long milliDate(int year, int week) { 130 Calendar calendar = Calendar.getInstance(); 131 calendar.set(year, 0, 0); 132 calendar.set(Calendar.WEEK_OF_YEAR, week); 133 Date d = calendar.getTime(); 134 return d.getTime(); 135 } 136 137 146 static public Date Date(int year, int week, int day) { 147 Calendar cal = Calendar.getInstance(); 148 cal.set(Calendar.YEAR, year); 149 cal.set(Calendar.WEEK_OF_YEAR, week); 150 cal.set(Calendar.DAY_OF_WEEK, day); 151 return cal.getTime(); 152 } 153 154 161 public static String makedbmdate(Date da) { 162 SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd", Locale.ENGLISH); 163 return f.format(da); 164 } 165 166 173 public static Date parsedbmdate(String wh) { 174 SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd", Locale.ENGLISH); 175 176 if (wh.indexOf('/') != -1) { 178 f = new SimpleDateFormat ("yyyy/MM/dd", Locale.ENGLISH); 179 } 180 try { 181 return f.parse(wh); 182 } catch (ParseException e) { 183 return new Date (); 185 } 186 } 187 188 194 public static String colontime(String time) { 195 if (time.length() == 4) { 196 return time.substring(0, 2) + ":" + time.substring(2, 4); 197 } 198 return time; 199 } 200 201 209 public static int parsedate(String sDate) { 210 SimpleDateFormat df = (SimpleDateFormat ) DateFormat.getDateTimeInstance(); 211 TimeZone tz; 212 df.applyLocalizedPattern("yyyyMMdd"); 213 214 tz = TimeZone.getDefault(); 215 df.setTimeZone(tz); 216 217 Date date = null; 218 try { 219 date = df.parse(sDate); 220 } catch (java.text.ParseException e) { 221 log.error(e.toString()); 222 } 223 224 if (date != null) 225 return (int) ((date.getTime() - getMilliOffset()) / 1000); 226 else 227 return -1; 228 } 229 230 238 public static int parsetime(String wh) { 239 int h = 0, m = 0, s = 0; 240 try { 241 h = Integer.parseInt(wh.substring(0, 2)); 242 m = Integer.parseInt(wh.substring(2, 4)); 243 s = Integer.parseInt(wh.substring(4, 6)); 244 } catch (Exception e) { 245 log.error("DateSupport: maketime (" + wh + ")"); 246 } 247 return s + 60 * (m + 60 * h); 248 } 249 250 258 public static int parsedatetime(String wh) { 259 return parsedate(wh.substring(0, 8)) + parsetime(wh.substring(8, 14)); 260 } 261 262 276 public static String getTime(int val) { 277 if (dooffset) { 278 val += offset; 279 } 280 Date v = new Date ((long) val * 1000); 281 SimpleDateFormat f = new SimpleDateFormat ("HH:mm"); 282 return f.format(v); 283 } 284 285 299 public static String getTimeSec(int val) { 300 Date v; 301 if (val == -1) { 302 v = new Date (); 304 } else { 305 if (dooffset) { 306 val += offset; 307 } 308 v = new Date ((long) val * 1000); 309 } 310 SimpleDateFormat f = new SimpleDateFormat ("HH:mm:ss"); 311 return f.format(v); 312 } 313 314 328 public static String getTimeSecLen(int val) { 329 Date date = new Date ((long) val * 1000); 330 SimpleDateFormat f = new SimpleDateFormat ("HH:mm:ss"); 331 f.setTimeZone(TimeZone.getTimeZone("GMT")); 332 return f.format(date); 333 } 334 335 349 public static String getMonthDay(int val) { 350 if (dooffset) { 351 val += offset; 352 } 353 Date v = new Date ((long) val * 1000); 354 SimpleDateFormat f = new SimpleDateFormat ("dd"); 355 return f.format(v); 356 } 357 358 372 public static String getMonth(int val) { 373 if (dooffset) { 374 val += offset; 375 } 376 Date v = new Date ((long) val * 1000); 377 SimpleDateFormat f = new SimpleDateFormat ("MM"); 378 return f.format(v); 379 } 380 381 395 public static String getYear(int val) { 396 if (dooffset) { 398 val += offset; 399 } 400 Date v = new Date (((long) val) * 1000); 401 Calendar c = Calendar.getInstance(); 402 c.setTime(v); 403 404 return Integer.toString(c.get(Calendar.YEAR)); 405 } 406 407 421 public static int getMonthInt(int val) { 422 if (dooffset) { 423 val += offset; 424 } 425 Date v = new Date ((long) val * 1000); 426 Calendar c = Calendar.getInstance(); 427 c.setTime(v); 428 return c.get(Calendar.MONTH); 429 } 430 431 446 public static int getWeekDayInt(int val) { 447 if (dooffset) { 448 val += offset; 449 } 450 Date v = new Date ((long) val * 1000); 451 Calendar c = Calendar.getInstance(); 452 c.setTime(v); 453 return c.get(Calendar.DAY_OF_WEEK)-1; 454 } 455 456 471 public static int getDayInt(int val) { 472 if (dooffset) { 473 val += offset; 474 } 475 Date v = new Date ((long) val * 1000); 476 Calendar c = Calendar.getInstance(); 477 c.setTime(v); 478 return c.get(Calendar.DAY_OF_MONTH); 479 } 480 481 486 public static long getMilliOffset() { 487 if (!dooffset) { 488 490 TimeZone tz1, tz2; 491 int off1, off2; 492 Date d = new Date (); 493 494 tz1 = TimeZone.getDefault(); tz2 = TimeZone.getTimeZone("ECT"); off1 = tz1.getRawOffset(); 497 off2 = tz2.getRawOffset(); 498 if (tz1.inDaylightTime(d)) { 499 off1 += (3600 * 1000); } 501 502 if (tz2.inDaylightTime(d)) { 503 off2 += (3600 * 1000); 504 } 505 return off1 - off2; 506 } else { 507 return (long) offset * 1000; 508 } 509 } 510 511 516 public static long currentTimeMillis() { 517 return System.currentTimeMillis() - getMilliOffset(); 518 } 519 520 526 public static long convertDateToLong(String date) { 527 log.debug("Converting " + date); 530 Calendar cal = Calendar.getInstance(); 531 TimeZone tz = TimeZone.getDefault(); 532 533 cal.setTimeZone(tz); 534 cal = parseDate(cal, date); 535 536 Date d = cal.getTime(); 537 long l = d.getTime(); 538 539 return l; 540 } 541 542 545 546 547 550 private static long convertStringToLongWithTimeZone(String date, int hour, int minutes) { 551 Calendar calendar = setTimeZone(hour, minutes); 553 554 calendar = parseDate(calendar, date); 556 557 Date mydate = calendar.getTime(); 559 560 return mydate.getTime(); 562 } 563 564 567 private static Calendar setTimeZone(int hours, int minutes) { 568 log.warn("obsolete setTimeZone was used!!"); 569 570 String [] ids = TimeZone.getAvailableIDs((hours * 60 + minutes) * 60 * 1000); 572 573 if (ids.length == 0) { 575 log.error("Timezone is wrong..."); 576 System.exit(0); } 578 System.out.println("Current Time"); 579 580 SimpleTimeZone pdt = new SimpleTimeZone ((hours * 60 + minutes) * 60 * 1000, ids[0]); 582 583 pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); 585 pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000); 586 587 Calendar calendar = new GregorianCalendar (pdt); 590 return calendar; 591 } 592 593 600 public static Calendar parseDate(Calendar cal, String date) { 601 StringTokenizer tok = new StringTokenizer (date, "-\n\r:/ "); 602 String token = null; 603 604 cal.clear(Calendar.HOUR_OF_DAY); 605 606 token = tok.nextToken(); 607 cal.set(Calendar.HOUR_OF_DAY, new Integer (token).intValue()); 608 token = tok.nextToken(); 609 cal.set(Calendar.MINUTE, new Integer (token).intValue()); 610 token = tok.nextToken(); 611 cal.set(Calendar.SECOND, new Integer (token).intValue()); 612 token = tok.nextToken(); 613 cal.set(Calendar.DAY_OF_MONTH, new Integer (token).intValue()); 614 token = tok.nextToken(); 615 cal.set(Calendar.MONTH, new Integer (token).intValue() - 1); 616 token = tok.nextToken(); 617 cal.set(Calendar.YEAR, new Integer (token).intValue()); 618 return (cal); 619 } 620 621 628 public static Calendar parseDateRev(Calendar cal, String date) { 629 StringTokenizer tok = new StringTokenizer (date, "-\n\r:/ "); 630 String token = null; 631 632 cal.clear(Calendar.HOUR_OF_DAY); 633 634 token = tok.nextToken(); 635 cal.set(Calendar.YEAR, new Integer (token).intValue()); 636 token = tok.nextToken(); 637 cal.set(Calendar.MONTH, new Integer (token).intValue() - 1); 638 token = tok.nextToken(); 639 cal.set(Calendar.DAY_OF_MONTH, new Integer (token).intValue()); 640 token = tok.nextToken(); 641 cal.set(Calendar.HOUR_OF_DAY, new Integer (token).intValue()); 642 token = tok.nextToken(); 643 cal.set(Calendar.MINUTE, new Integer (token).intValue()); 644 token = tok.nextToken(); 645 cal.set(Calendar.SECOND, new Integer (token).intValue()); 646 return (cal); 647 } 648 649 656 public static String date2string(int time) { 657 return getTimeSec(time) + " " + getMonthDay(time) + "/" + getMonth(time) + "/" + getYear(time); 658 } 659 660 667 public static String date2day(int time) { 668 return getYear(time) + "-" + getMonth(time) + "-" + getMonthDay(time); 669 } 670 671 678 public static String date2date(int time) { 679 return getYear(time) + "-" + getMonth(time) + "-" + getMonthDay(time) + " " + getTimeSec(time); 680 } 681 682 687 private static String dumpdate(int d) { 688 Date dd = new Date ((long) d * 1000); 689 return dd.toString(); 690 } 691 692 696 public static void main(String args[]) { 697 log.info("Date (without corr)" + date2string((int) (System.currentTimeMillis() / 1000)) + " " + System.currentTimeMillis() / 1000); 698 log.info("Date (with corr)" + date2string((int) (DateSupport.currentTimeMillis() / 1000)) + " : " + DateSupport.currentTimeMillis() / 1000); 699 log.info("Date " + args[0] + " " + date2string(Integer.parseInt(args[0]))); 700 log.info("Date " + args[0] + " " + dumpdate(Integer.parseInt(args[0]))); 701 String ID = System.getProperty("user.timezone", "GMT"); 702 log.info("ID " + ID + " : " + getMilliOffset()); 703 log.info("ParseDate " + parsedate(args[1])); 704 } 705 } 706 | Popular Tags |