KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > util > RelativeDateFormat


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -----------------------
28  * RelativeDateFormat.java
29  * -----------------------
30  * (C) Copyright 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: RelativeDateFormat.java,v 1.1.2.1 2006/11/17 10:48:57 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 01-Nov-2006 : Version 1 (DG);
40  *
41  */

42 package org.jfree.chart.util;
43
44 import java.text.DateFormat JavaDoc;
45 import java.text.DecimalFormat JavaDoc;
46 import java.text.FieldPosition JavaDoc;
47 import java.text.NumberFormat JavaDoc;
48 import java.text.ParsePosition JavaDoc;
49 import java.util.Calendar JavaDoc;
50 import java.util.Date JavaDoc;
51 import java.util.GregorianCalendar JavaDoc;
52
53 /**
54  * A formatter that formats dates to show the elapsed time relative to some
55  * base date.
56  *
57  * @since 1.0.3
58  */

59 public class RelativeDateFormat extends DateFormat JavaDoc {
60     
61     /** The base milliseconds for the elapsed time calculation. */
62     private long baseMillis;
63     
64     /**
65      * A flag that controls whether or not a zero day count is displayed.
66      */

67     private boolean showZeroDays;
68     
69     /**
70      * A formatter for the day count (most likely not critical until the
71      * day count exceeds 999).
72      */

73     private NumberFormat JavaDoc dayFormatter;
74     
75     /**
76      * A string appended after the day count.
77      */

78     private String JavaDoc daySuffix;
79     
80     /**
81      * A string appended after the hours.
82      */

83     private String JavaDoc hourSuffix;
84     
85     /**
86      * A string appended after the minutes.
87      */

88     private String JavaDoc minuteSuffix;
89     
90     /**
91      * A formatter for the seconds (and milliseconds).
92      */

93     private NumberFormat JavaDoc secondFormatter;
94     
95     /**
96      * A string appended after the seconds.
97      */

98     private String JavaDoc secondSuffix;
99
100     /**
101      * A constant for the number of milliseconds in one hour.
102      */

103     private static long MILLISECONDS_IN_ONE_HOUR = 60 * 60 * 1000L;
104
105     /**
106      * A constant for the number of milliseconds in one day.
107      */

108     private static long MILLISECONDS_IN_ONE_DAY = 24 * MILLISECONDS_IN_ONE_HOUR;
109     
110     /**
111      * Creates a new instance.
112      */

113     public RelativeDateFormat() {
114         this(0L);
115     }
116     
117     /**
118      * Creates a new instance.
119      *
120      * @param time the date/time (<code>null</code> not permitted).
121      */

122     public RelativeDateFormat(Date JavaDoc time) {
123         this(time.getTime());
124     }
125     
126     /**
127      * Creates a new instance.
128      *
129      * @param baseMillis the time zone (<code>null</code> not permitted).
130      */

131     public RelativeDateFormat(long baseMillis) {
132         super();
133         this.baseMillis = baseMillis;
134         this.showZeroDays = false;
135         this.dayFormatter = NumberFormat.getInstance();
136         this.daySuffix = "d";
137         this.hourSuffix = "h";
138         this.minuteSuffix = "m";
139         this.secondFormatter = NumberFormat.getNumberInstance();
140         this.secondFormatter.setMaximumFractionDigits(3);
141         this.secondFormatter.setMinimumFractionDigits(3);
142         this.secondSuffix = "s";
143     }
144     
145     /**
146      * Returns the base date/time used to calculate the elapsed time for
147      * display.
148      *
149      * @return The base date/time in milliseconds since 1-Jan-1970.
150      *
151      * @see #setBaseMillis(long)
152      */

153     public long getBaseMillis() {
154         return this.baseMillis;
155     }
156     
157     /**
158      * Sets the base date/time used to calculate the elapsed time for display.
159      * This should be specified in milliseconds using the same encoding as
160      * <code>java.util.Date</code>.
161      *
162      * @param baseMillis the base date/time in milliseconds.
163      *
164      * @see #getBaseMillis()
165      */

166     public void setBaseMillis(long baseMillis) {
167         this.baseMillis = baseMillis;
168     }
169     
170     /**
171      * Returns the flag that controls whether or not zero day counts are
172      * shown in the formatted output.
173      *
174      * @return The flag.
175      *
176      * @see #setShowZeroDays(boolean)
177      */

178     public boolean getShowZeroDays() {
179         return this.showZeroDays;
180     }
181     
182     /**
183      * Sets the flag that controls whether or not zero day counts are shown
184      * in the formatted output.
185      *
186      * @param show the flag.
187      *
188      * @see #getShowZeroDays()
189      */

190     public void setShowZeroDays(boolean show) {
191         this.showZeroDays = show;
192     }
193     
194     /**
195      * Returns the string that is appended to the day count.
196      *
197      * @return The string.
198      *
199      * @see #setDaySuffix(String)
200      */

201     public String JavaDoc getDaySuffix() {
202         return this.daySuffix;
203     }
204     
205     /**
206      * Sets the string that is appended to the day count.
207      *
208      * @param suffix the suffix.
209      *
210      * @see #getDaySuffix()
211      */

212     public void setDaySuffix(String JavaDoc suffix) {
213         this.daySuffix = suffix;
214     }
215
216     /**
217      * Returns the string that is appended to the hour count.
218      *
219      * @return The string.
220      *
221      * @see #setHourSuffix(String)
222      */

223     public String JavaDoc getHourSuffix() {
224         return this.hourSuffix;
225     }
226     
227     /**
228      * Sets the string that is appended to the hour count.
229      *
230      * @param suffix the suffix.
231      *
232      * @see #getHourSuffix()
233      */

234     public void setHourSuffix(String JavaDoc suffix) {
235         this.hourSuffix = suffix;
236     }
237
238     /**
239      * Returns the string that is appended to the minute count.
240      *
241      * @return The string.
242      *
243      * @see #setMinuteSuffix(String)
244      */

245     public String JavaDoc getMinuteSuffix() {
246         return this.minuteSuffix;
247     }
248     
249     /**
250      * Sets the string that is appended to the minute count.
251      *
252      * @param suffix the suffix.
253      *
254      * @see #getMinuteSuffix()
255      */

256     public void setMinuteSuffix(String JavaDoc suffix) {
257         this.minuteSuffix = suffix;
258     }
259
260     /**
261      * Returns the string that is appended to the second count.
262      *
263      * @return The string.
264      *
265      * @see #setSecondSuffix(String)
266      */

267     public String JavaDoc getSecondSuffix() {
268         return this.secondSuffix;
269     }
270     
271     /**
272      * Sets the string that is appended to the second count.
273      *
274      * @param suffix the suffix.
275      *
276      * @see #getSecondSuffix()
277      */

278     public void setSecondSuffix(String JavaDoc suffix) {
279         this.secondSuffix = suffix;
280     }
281     
282     /**
283      * Sets the formatter for the seconds and milliseconds.
284      *
285      * @param formatter the formatter (<code>null</code> not permitted).
286      */

287     public void setSecondFormatter(NumberFormat JavaDoc formatter) {
288         if (formatter == null) {
289             throw new IllegalArgumentException JavaDoc("Null 'formatter' argument.");
290         }
291         this.secondFormatter = formatter;
292     }
293
294     /**
295      * Formats the given date as the amount of elapsed time (relative to the
296      * base date specified in the constructor).
297      *
298      * @param date the date.
299      * @param toAppendTo the string buffer.
300      * @param fieldPosition the field position.
301      *
302      * @return The formatted date.
303      */

304     public StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc toAppendTo,
305                                FieldPosition JavaDoc fieldPosition) {
306         long currentMillis = date.getTime();
307         long elapsed = currentMillis - baseMillis;
308         
309         long days = elapsed / MILLISECONDS_IN_ONE_DAY;
310         elapsed = elapsed - (days * MILLISECONDS_IN_ONE_DAY);
311         long hours = elapsed / MILLISECONDS_IN_ONE_HOUR;
312         elapsed = elapsed - (hours * MILLISECONDS_IN_ONE_HOUR);
313         long minutes = elapsed / 60000L;
314         elapsed = elapsed - (minutes * 60000L);
315         double seconds = elapsed / 1000.0;
316         if (days != 0 || this.showZeroDays) {
317             toAppendTo.append(this.dayFormatter.format(days) + getDaySuffix());
318         }
319         toAppendTo.append(String.valueOf(hours) + getHourSuffix());
320         toAppendTo.append(String.valueOf(minutes) + getMinuteSuffix());
321         toAppendTo.append(this.secondFormatter.format(seconds)
322                 + getSecondSuffix());
323         return toAppendTo;
324     }
325
326     /**
327      * Parses the given string (not implemented).
328      *
329      * @param source the date string.
330      * @param pos the parse position.
331      *
332      * @return <code>null</code>, as this method has not been implemented.
333      */

334     public Date JavaDoc parse(String JavaDoc source, ParsePosition JavaDoc pos) {
335         return null;
336     }
337
338     /**
339      * Tests this formatter for equality with an arbitrary object.
340      *
341      * @param obj the object.
342      *
343      * @return A boolean.
344      */

345     public boolean equals(Object JavaDoc obj) {
346         if (obj == this) {
347             return true;
348         }
349         if (!(obj instanceof RelativeDateFormat)) {
350             return false;
351         }
352         if (!super.equals(obj)) {
353             return false;
354         }
355         RelativeDateFormat that = (RelativeDateFormat) obj;
356         if (this.baseMillis != that.baseMillis) {
357             return false;
358         }
359         return true;
360     }
361
362     /**
363      * Some test code.
364      *
365      * @param args ignored.
366      */

367     public static void main(String JavaDoc[] args) {
368         GregorianCalendar JavaDoc c0 = new GregorianCalendar JavaDoc(2006, 10, 1, 0, 0, 0);
369         GregorianCalendar JavaDoc c1 = new GregorianCalendar JavaDoc(2006, 10, 1, 11, 37, 43);
370         c1.set(Calendar.MILLISECOND, 123);
371         
372         System.out.println("Default: ");
373         RelativeDateFormat rdf = new RelativeDateFormat(c0.getTimeInMillis());
374         System.out.println(rdf.format(c1.getTime()));
375         System.out.println();
376         
377         System.out.println("Hide milliseconds: ");
378         rdf.setSecondFormatter(new DecimalFormat JavaDoc("0"));
379         System.out.println(rdf.format(c1.getTime()));
380         System.out.println();
381
382         System.out.println("Show zero day output: ");
383         rdf.setShowZeroDays(true);
384         System.out.println(rdf.format(c1.getTime()));
385         System.out.println();
386         
387         System.out.println("Alternative suffixes: ");
388         rdf.setShowZeroDays(false);
389         rdf.setDaySuffix(":");
390         rdf.setHourSuffix(":");
391         rdf.setMinuteSuffix(":");
392         rdf.setSecondSuffix("");
393         System.out.println(rdf.format(c1.getTime()));
394         System.out.println();
395     }
396 }
397
Popular Tags