KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > TimeFormat


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.mx.util;
23
24
25 import java.io.Serializable JavaDoc;
26
27 /**
28  * TimeFormat is a utility class for converting a long into a
29  * human readable string.
30  *
31  * <P>
32  *
33  * Example usage:
34  *
35  * <CODE>
36  * System.out.println("You have been online for: "+TimeFormat.valueOf(milliseconds));
37  * </CODE>
38  *
39  * FIXME: expanded features need documentation. JGH
40  *
41  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
42  * @date $Date: 2005-10-29 20:04:02 -0400 (Sat, 29 Oct 2005) $
43  * @version $Revision: 37459 $
44  */

45 public final class TimeFormat implements Serializable JavaDoc
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     /**
73      * construct a time format
74      *
75      *
76      * @param milliseconds
77      */

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             // if less than second, we'll just
94
// display
95
time = milliseconds;
96         }
97     }
98
99     /**
100      * construct a time format
101      *
102      *
103      * @param milliseconds
104      */

105     private TimeFormat(long milliseconds)
106     {
107         this(milliseconds, TimeFormat.ROUND_TO_MILLISECOND);
108     }
109
110     /**
111      * get days
112      *
113      * @return days
114      */

115     public long getDays()
116     {
117         return days;
118     }
119
120     /**
121      * get minutes
122      *
123      * @return minutes
124      */

125     public long getMinutes()
126     {
127         return minutes;
128     }
129
130     /**
131      * get hours
132      *
133      * @return hours
134      */

135     public long getHours()
136     {
137         return hours;
138     }
139
140     /**
141      * get seconds
142      *
143      * @return seconds
144      */

145     public long getSeconds()
146     {
147         return seconds;
148     }
149
150     /**
151      * add a timeformat
152      *
153      *
154      * @param t
155      */

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     /**
165      * get days from a time format
166      *
167      *
168      * @param t
169      */

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     /**
180      * get hours from a time format
181      *
182      *
183      * @param t
184      */

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     /**
195      * get minutes from a time format
196      *
197      *
198      * @param t
199      */

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     /**
210      * get seconds from a time format
211      *
212      *
213      * @param t
214      */

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     /**
230      * update time
231      *
232      *
233      * @param t
234      */

235     public void getTime(TimeFormat t)
236     {
237         t.getTime();
238     }
239
240     /**
241      * update
242      *
243      */

244     private void getTime()
245     {
246         getDays(this);
247         getHours(this);
248         getMinutes(this);
249         getSeconds(this);
250     }
251
252     /**
253      * get the milliseconds
254      */

255     public long getMilliseconds()
256     {
257         return (micro ? time : milliseconds);
258     }
259
260     /**
261      * print out the time format in a string representation
262      */

263     public String JavaDoc toString()
264     {
265         return format(rounding);
266     }
267
268     /**
269      * set rounding - one of ROUND_TO_MILLISECONDS, etc.
270      */

271     public void setRounding(int r)
272     {
273         rounding = r;
274     }
275
276     /**
277      * return the rounding
278      */

279     public int getRouding()
280     {
281         return rounding;
282     }
283
284     /**
285      * format string based on rouding
286      */

287     public String JavaDoc 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     /**
337      * FIXME: Missing Method declaration
338      *
339      *
340      * @param empty
341      * @return
342      */

343     private String JavaDoc 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     /**
354      * FIXME: Missing Method declaration
355      *
356      *
357      * @param empty
358      * @return
359      */

360     private String JavaDoc 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     /**
371      * FIXME: Missing Method declaration
372      *
373      *
374      * @param empty
375      * @return
376      */

377     private String JavaDoc 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     /**
388      * FIXME: Missing Method declaration
389      *
390      *
391      * @param empty
392      * @return
393      */

394     private String JavaDoc 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     /**
405      * handle amt formatting
406      */

407     private String JavaDoc format(String JavaDoc single, String JavaDoc plural, long amt)
408     {
409         if (amt > 0)
410         {
411             return amt + " " + (amt > 1 ? plural : single) + " ";
412         }
413
414         return "";
415     }
416
417     /**
418      * return a string formatted version of time <code>t</code>
419      * rounding to <code>round</code>
420      *
421      * @param t
422      * @param round
423      * @return String value
424      */

425     public static String JavaDoc valueOf(long t, int round)
426     {
427         TimeFormat f = new TimeFormat(t, round);
428
429         return f.toString();
430     }
431
432     /**
433      * return a string formatted version of time <code>t</code>
434      * rounding to <code>round</code>
435      *
436      * @param t
437      * @param round
438      * @return String value
439      */

440     public static String JavaDoc valueOf(long t)
441     {
442         return valueOf(t, TimeFormat.ROUND_TO_MILLISECOND);
443     }
444
445     /**
446      * format with a date time
447      */

448     public static String JavaDoc format(String JavaDoc 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     /**
457      * parse
458      */

459     private String JavaDoc parse(String JavaDoc format, long day, long hour, long minute, long second, long millis)
460     {
461         String JavaDoc 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': // days
491

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': // hours
503

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': // minutes
515

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': // seconds
527

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': // milliseconds
539

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     /**
558      * zero pad a number to len
559      */

560     private String JavaDoc zeroPad(long value, int len)
561     {
562         String JavaDoc s = String.valueOf(value);
563         int l = s.length();
564         String JavaDoc r = "";
565
566         for (int c = l; c <= len; c++)
567         {
568             r += "0";
569         }
570
571         return r + s;
572     }
573
574     /**
575      * test
576      *
577      *
578      * @param args
579      */

580     public static void main(String JavaDoc args[])
581     {
582         String JavaDoc 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 /**
615  * $Log:
616  * 1 Head - DO NOT USE1.0 12/3/01 2:51:16 PM jhaynie
617  * $
618  * Revision 1.2 2001/08/31 22:04:24 jhaynie
619  * added parsing and formatting features
620  *
621  * Revision 1.1 2001/08/29 19:47:53 jhaynie
622  * initial checkin
623  *
624  */

625
Popular Tags