1 22 package org.jboss.mx.util; 23 24 25 import java.io.Serializable ; 26 27 45 public final class TimeFormat implements Serializable 46 { 47 public static final boolean DEBUG = false; 48 49 public static final long ONE_MILLISECOND = (1); 50 public static final long ONE_SECOND = (ONE_MILLISECOND * 1000); 51 public static final long ONE_MINUTE = (ONE_SECOND * 60); 52 public static final long ONE_HOUR = (ONE_MINUTE * 60); 53 public static final long ONE_DAY = (ONE_HOUR * 24); 54 55 public static final int ROUND_TO_MILLISECOND = 5; 56 public static final int ROUND_TO_SECOND = 4; 57 public static final int ROUND_TO_MINUTE = 3; 58 public static final int ROUND_TO_HOUR = 2; 59 public static final int ROUND_TO_DAY = 1; 60 61 private long original = 0; 62 private long time = 0; 63 private long remainder = 0; 64 private long days = 0; 65 private long hours = 0; 66 private long minutes = 0; 67 private long seconds = 0; 68 private long milliseconds = 0; 69 private boolean micro = false; 70 private int rounding = ROUND_TO_SECOND; 71 72 78 private TimeFormat(long milliseconds, int round) 79 { 80 this.rounding = round; 81 this.original = milliseconds; 82 83 if (milliseconds >= ONE_SECOND) 84 { 85 this.remainder = milliseconds; 86 87 getTime(); 88 } 89 else 90 { 91 micro = true; 92 93 time = milliseconds; 96 } 97 } 98 99 105 private TimeFormat(long milliseconds) 106 { 107 this(milliseconds, TimeFormat.ROUND_TO_MILLISECOND); 108 } 109 110 115 public long getDays() 116 { 117 return days; 118 } 119 120 125 public long getMinutes() 126 { 127 return minutes; 128 } 129 130 135 public long getHours() 136 { 137 return hours; 138 } 139 140 145 public long getSeconds() 146 { 147 return seconds; 148 } 149 150 156 public void add(TimeFormat t) 157 { 158 days += t.days; 159 hours += t.hours; 160 minutes += t.minutes; 161 seconds += t.seconds; 162 } 163 164 170 public void getDays(TimeFormat t) 171 { 172 if (t.remainder >= ONE_DAY) 173 { 174 t.days = (t.remainder / ONE_DAY); 175 t.remainder -= (t.days * ONE_DAY); 176 } 177 } 178 179 185 public void getHours(TimeFormat t) 186 { 187 if (t.remainder >= ONE_HOUR && t.remainder < ONE_DAY) 188 { 189 t.hours = (t.remainder / ONE_HOUR); 190 t.remainder -= (t.hours * ONE_HOUR); 191 } 192 } 193 194 200 public void getMinutes(TimeFormat t) 201 { 202 if (t.remainder >= ONE_MINUTE && t.remainder < ONE_HOUR) 203 { 204 t.minutes = (t.remainder / ONE_MINUTE); 205 t.remainder -= (t.minutes * ONE_MINUTE); 206 } 207 } 208 209 215 public void getSeconds(TimeFormat t) 216 { 217 if (t.remainder >= ONE_SECOND && t.remainder < ONE_MINUTE) 218 { 219 t.seconds = (t.remainder / ONE_SECOND); 220 t.milliseconds = t.remainder -= (t.seconds * ONE_SECOND); 221 } 222 else 223 { 224 t.seconds = 0; 225 t.milliseconds = t.remainder; 226 } 227 } 228 229 235 public void getTime(TimeFormat t) 236 { 237 t.getTime(); 238 } 239 240 244 private void getTime() 245 { 246 getDays(this); 247 getHours(this); 248 getMinutes(this); 249 getSeconds(this); 250 } 251 252 255 public long getMilliseconds() 256 { 257 return (micro ? time : milliseconds); 258 } 259 260 263 public String toString() 264 { 265 return format(rounding); 266 } 267 268 271 public void setRounding(int r) 272 { 273 rounding = r; 274 } 275 276 279 public int getRouding() 280 { 281 return rounding; 282 } 283 284 287 public String format(int round) 288 { 289 290 if (DEBUG) 291 { 292 System.err.println("-->time: " + time + ", round: " + round + ", micro: " + micro + ",remainder:" 293 + remainder); 294 System.err.println("-->days: " + days); 295 System.err.println("-->hours: " + hours); 296 System.err.println("-->minutes: " + minutes); 297 System.err.println("-->hours: " + hours); 298 System.err.println("-->seconds: " + seconds); 299 System.err.println("-->milliseconds: " + milliseconds); 300 System.err.flush(); 301 } 302 303 switch (round) 304 { 305 306 case ROUND_TO_DAY: 307 { 308 return formatDays(false); 309 } 310 311 case ROUND_TO_HOUR: 312 { 313 return formatDays(true) + formatHours(false); 314 } 315 316 case ROUND_TO_MINUTE: 317 { 318 return formatDays(true) + formatHours(true) + formatMinutes(false); 319 } 320 321 case ROUND_TO_SECOND: 322 { 323 return formatDays(true) + formatHours(true) + formatMinutes(true) + formatSeconds(false); 324 } 325 326 case ROUND_TO_MILLISECOND: 327 { 328 return formatDays(true) + formatHours(true) + formatMinutes(true) + formatSeconds(true) 329 + (micro ? time : milliseconds) + " ms"; 330 } 331 } 332 333 return original + " ms"; 334 } 335 336 343 private String formatDays(boolean empty) 344 { 345 if (days <= 0) 346 { 347 return empty ? "" : "0 days"; 348 } 349 350 return format("day", "days", days); 351 } 352 353 360 private String formatHours(boolean empty) 361 { 362 if (hours <= 0) 363 { 364 return empty ? "" : "0 hours"; 365 } 366 367 return format("hour", "hours", hours); 368 } 369 370 377 private String formatMinutes(boolean empty) 378 { 379 if (minutes <= 0) 380 { 381 return empty ? "" : "0 minutes"; 382 } 383 384 return format("minute", "minutes", minutes); 385 } 386 387 394 private String formatSeconds(boolean empty) 395 { 396 if (seconds <= 0) 397 { 398 return empty ? "" : "0 seconds"; 399 } 400 401 return format("second", "seconds", seconds); 402 } 403 404 407 private String format(String single, String plural, long amt) 408 { 409 if (amt > 0) 410 { 411 return amt + " " + (amt > 1 ? plural : single) + " "; 412 } 413 414 return ""; 415 } 416 417 425 public static String valueOf(long t, int round) 426 { 427 TimeFormat f = new TimeFormat(t, round); 428 429 return f.toString(); 430 } 431 432 440 public static String valueOf(long t) 441 { 442 return valueOf(t, TimeFormat.ROUND_TO_MILLISECOND); 443 } 444 445 448 public static String format(String format, long time) 449 { 450 TimeFormat f = new TimeFormat(time); 451 452 return f.parse(format, f.getDays(), f.getHours(), f.getMinutes(), f.getSeconds(), f.getMilliseconds()); 453 454 } 455 456 459 private String parse(String format, long day, long hour, long minute, long second, long millis) 460 { 461 String s = ""; 462 int start = 0; 463 int len = format.length(); 464 465 for (int c = 0; c < len; c++) 466 { 467 char tc = format.charAt(c); 468 int sc = c; 469 int l = 0; 470 471 switch (tc) 472 { 473 474 case ' ': 475 { 476 s += " "; 477 478 break; 479 } 480 481 case '\'': 482 { 483 while (++c < len && format.charAt(c) != '\'') ; 484 485 s += format.substring(sc + 1, c); 486 487 break; 488 } 489 490 case 'D': 492 case 'd': 493 while (++c < len && (format.charAt(c) == 'd' || format.charAt(c) == 'D')) ; 494 495 l = c - sc; 496 s += sc <= 0 || start < 0 ? "" : format.substring(start, sc); 497 s += zeroPad(day, l - 1); 498 --c; 499 500 break; 501 502 case 'h': 504 case 'H': 505 while (++c < len && (format.charAt(c) == 'h' || format.charAt(c) == 'H')) ; 506 507 l = c - sc; 508 s += sc <= 0 || start < 0 ? "" : format.substring(start, sc); 509 s += zeroPad(hour, l - 1); 510 --c; 511 512 break; 513 514 case 'm': 516 case 'M': 517 while (++c < len && (format.charAt(c) == 'm' || format.charAt(c) == 'M')) ; 518 519 l = c - sc; 520 s += sc <= 0 || start < 0 ? "" : format.substring(start, sc); 521 s += zeroPad(minute, l - 1); 522 --c; 523 524 break; 525 526 case 's': 528 case 'S': 529 while (++c < len && (format.charAt(c) == 's' || format.charAt(c) == 'S')) ; 530 531 l = c - sc; 532 s += sc <= 0 || start < 0 ? "" : format.substring(start, sc); 533 s += zeroPad(second, l - 1); 534 --c; 535 536 break; 537 538 case 'z': 540 case 'Z': 541 while (++c < len && (format.charAt(c) == 'z' || format.charAt(c) == 'Z')) ; 542 543 l = c - sc; 544 s += sc <= 0 || start < 0 ? "" : format.substring(start, sc); 545 s += zeroPad(millis, l - 1); 546 --c; 547 548 break; 549 } 550 551 start = c + 1; 552 } 553 554 return s; 555 } 556 557 560 private String zeroPad(long value, int len) 561 { 562 String s = String.valueOf(value); 563 int l = s.length(); 564 String r = ""; 565 566 for (int c = l; c <= len; c++) 567 { 568 r += "0"; 569 } 570 571 return r + s; 572 } 573 574 580 public static void main(String args[]) 581 { 582 String FORMAT = "D 'days,' HH 'hours,' mm 'minutes and ' ss 'seconds, 'zz 'milliseconds'"; 583 584 System.out.println(TimeFormat.format(FORMAT, 1000)); 585 System.out.println("ONE SECOND: " + TimeFormat.ONE_SECOND); 586 System.out.println("ONE MINUTE: " + TimeFormat.ONE_MINUTE); 587 System.out.println("ONE HOUR: " + TimeFormat.ONE_HOUR); 588 System.out.println("ONE DAY: " + TimeFormat.ONE_DAY); 589 590 for (int c = 0; c <= 5; c++) 591 { 592 System.out.println("============ Round to: " + c + " =================="); 593 System.out.println("Time: " + TimeFormat.valueOf(Long.MAX_VALUE, c)); 594 System.out.println("Time: " + TimeFormat.valueOf(1236371400, c)); 595 System.out.println("Time: " + TimeFormat.format(FORMAT, 1236371400)); 596 System.out.println("Time: " + TimeFormat.valueOf(123613700, c)); 597 System.out.println("Time: " + TimeFormat.valueOf(700, c)); 598 System.out.println("Time: " + TimeFormat.valueOf(2001, c)); 599 System.out.println("Time: " + TimeFormat.valueOf(2101, c)); 600 System.out.println("Time: " + TimeFormat.valueOf(15, c)); 601 System.out.println("Time: " + TimeFormat.valueOf(999, c)); 602 System.out.println("Time: " + TimeFormat.valueOf(10000, c)); 603 System.out.println("Time: " + TimeFormat.valueOf(ONE_MINUTE * 10, c)); 604 System.out.println("Time: " + TimeFormat.valueOf(ONE_DAY * 10 + 101, c)); 605 System.out.println("Time: " + TimeFormat.valueOf(ONE_HOUR * 10, c)); 606 System.out.println("Time: " + TimeFormat.valueOf(ONE_HOUR + ONE_DAY + (ONE_MINUTE * 2), c)); 607 System.out.println("Time: " + TimeFormat.format(FORMAT, ONE_HOUR + ONE_DAY + (ONE_MINUTE * 2))); 608 System.out.println("================================================"); 609 } 610 } 611 612 } 613 614 625 | Popular Tags |