1:
44: package ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55:
61: public class RelativeDateFormat extends DateFormat {
62:
63:
64: private long baseMillis;
65:
66:
69: private boolean showZeroDays;
70:
71:
75: private NumberFormat dayFormatter;
76:
77:
80: private String daySuffix;
81:
82:
85: private String hourSuffix;
86:
87:
90: private String minuteSuffix;
91:
92:
95: private NumberFormat secondFormatter;
96:
97:
100: private String secondSuffix;
101:
102:
105: private static long MILLISECONDS_IN_ONE_HOUR = 60 * 60 * 1000L;
106:
107:
110: private static long MILLISECONDS_IN_ONE_DAY = 24 * MILLISECONDS_IN_ONE_HOUR;
111:
112:
115: public RelativeDateFormat() {
116: this(0L);
117: }
118:
119:
124: public RelativeDateFormat(Date time) {
125: this(time.getTime());
126: }
127:
128:
133: public RelativeDateFormat(long baseMillis) {
134: super();
135: this.baseMillis = baseMillis;
136: this.showZeroDays = false;
137: this.dayFormatter = NumberFormat.getInstance();
138: this.daySuffix = "d";
139: this.hourSuffix = "h";
140: this.minuteSuffix = "m";
141: this.secondFormatter = NumberFormat.getNumberInstance();
142: this.secondFormatter.setMaximumFractionDigits(3);
143: this.secondFormatter.setMinimumFractionDigits(3);
144: this.secondSuffix = "s";
145:
146:
147:
148: this.calendar = new GregorianCalendar();
149: this.numberFormat = new DecimalFormat("0");
150: }
151:
152:
160: public long getBaseMillis() {
161: return this.baseMillis;
162: }
163:
164:
173: public void setBaseMillis(long baseMillis) {
174: this.baseMillis = baseMillis;
175: }
176:
177:
185: public boolean getShowZeroDays() {
186: return this.showZeroDays;
187: }
188:
189:
197: public void setShowZeroDays(boolean show) {
198: this.showZeroDays = show;
199: }
200:
201:
208: public String getDaySuffix() {
209: return this.daySuffix;
210: }
211:
212:
219: public void setDaySuffix(String suffix) {
220: if (suffix == null) {
221: throw new IllegalArgumentException("Null 'suffix' argument.");
222: }
223: this.daySuffix = suffix;
224: }
225:
226:
233: public String getHourSuffix() {
234: return this.hourSuffix;
235: }
236:
237:
244: public void setHourSuffix(String suffix) {
245: if (suffix == null) {
246: throw new IllegalArgumentException("Null 'suffix' argument.");
247: }
248: this.hourSuffix = suffix;
249: }
250:
251:
258: public String getMinuteSuffix() {
259: return this.minuteSuffix;
260: }
261:
262:
269: public void setMinuteSuffix(String suffix) {
270: if (suffix == null) {
271: throw new IllegalArgumentException("Null 'suffix' argument.");
272: }
273: this.minuteSuffix = suffix;
274: }
275:
276:
283: public String getSecondSuffix() {
284: return this.secondSuffix;
285: }
286:
287:
294: public void setSecondSuffix(String suffix) {
295: if (suffix == null) {
296: throw new IllegalArgumentException("Null 'suffix' argument.");
297: }
298: this.secondSuffix = suffix;
299: }
300:
301:
306: public void setSecondFormatter(NumberFormat formatter) {
307: if (formatter == null) {
308: throw new IllegalArgumentException("Null 'formatter' argument.");
309: }
310: this.secondFormatter = formatter;
311: }
312:
313:
323: public StringBuffer format(Date date, StringBuffer toAppendTo,
324: FieldPosition fieldPosition) {
325: long currentMillis = date.getTime();
326: long elapsed = currentMillis - this.baseMillis;
327:
328: long days = elapsed / MILLISECONDS_IN_ONE_DAY;
329: elapsed = elapsed - (days * MILLISECONDS_IN_ONE_DAY);
330: long hours = elapsed / MILLISECONDS_IN_ONE_HOUR;
331: elapsed = elapsed - (hours * MILLISECONDS_IN_ONE_HOUR);
332: long minutes = elapsed / 60000L;
333: elapsed = elapsed - (minutes * 60000L);
334: double seconds = elapsed / 1000.0;
335: if (days != 0 || this.showZeroDays) {
336: toAppendTo.append(this.dayFormatter.format(days) + getDaySuffix());
337: }
338: toAppendTo.append(String.valueOf(hours) + getHourSuffix());
339: toAppendTo.append(String.valueOf(minutes) + getMinuteSuffix());
340: toAppendTo.append(this.secondFormatter.format(seconds)
341: + getSecondSuffix());
342: return toAppendTo;
343: }
344:
345:
353: public Date parse(String source, ParsePosition pos) {
354: return null;
355: }
356:
357:
364: public boolean equals(Object obj) {
365: if (obj == this) {
366: return true;
367: }
368: if (!(obj instanceof RelativeDateFormat)) {
369: return false;
370: }
371: if (!super.equals(obj)) {
372: return false;
373: }
374: RelativeDateFormat that = (RelativeDateFormat) obj;
375: if (this.baseMillis != that.baseMillis) {
376: return false;
377: }
378: if (this.showZeroDays != that.showZeroDays) {
379: return false;
380: }
381: if (!this.daySuffix.equals(that.daySuffix)) {
382: return false;
383: }
384: if (!this.hourSuffix.equals(that.hourSuffix)) {
385: return false;
386: }
387: if (!this.minuteSuffix.equals(that.minuteSuffix)) {
388: return false;
389: }
390: if (!this.secondSuffix.equals(that.secondSuffix)) {
391: return false;
392: }
393: if (!this.secondFormatter.equals(that.secondFormatter)) {
394: return false;
395: }
396: return true;
397: }
398:
399:
404: public int hashCode() {
405: int result = 193;
406: result = 37 * result
407: + (int) (this.baseMillis ^ (this.baseMillis >>> 32));
408: result = 37 * result + this.daySuffix.hashCode();
409: result = 37 * result + this.hourSuffix.hashCode();
410: result = 37 * result + this.minuteSuffix.hashCode();
411: result = 37 * result + this.secondSuffix.hashCode();
412: result = 37 * result + this.secondFormatter.hashCode();
413: return result;
414: }
415:
416:
421: public Object clone() {
422: RelativeDateFormat clone = (RelativeDateFormat) super.clone();
423: clone.dayFormatter = (NumberFormat) this.dayFormatter.clone();
424: clone.secondFormatter = (NumberFormat) this.secondFormatter.clone();
425: return clone;
426: }
427:
428:
433: public static void main(String[] args) {
434: GregorianCalendar c0 = new GregorianCalendar(2006, 10, 1, 0, 0, 0);
435: GregorianCalendar c1 = new GregorianCalendar(2006, 10, 1, 11, 37, 43);
436: c1.set(Calendar.MILLISECOND, 123);
437:
438: System.out.println("Default: ");
439: RelativeDateFormat rdf = new RelativeDateFormat(c0.getTimeInMillis());
440: System.out.println(rdf.format(c1.getTime()));
441: System.out.println();
442:
443: System.out.println("Hide milliseconds: ");
444: rdf.setSecondFormatter(new DecimalFormat("0"));
445: System.out.println(rdf.format(c1.getTime()));
446: System.out.println();
447:
448: System.out.println("Show zero day output: ");
449: rdf.setShowZeroDays(true);
450: System.out.println(rdf.format(c1.getTime()));
451: System.out.println();
452:
453: System.out.println("Alternative suffixes: ");
454: rdf.setShowZeroDays(false);
455: rdf.setDaySuffix(":");
456: rdf.setHourSuffix(":");
457: rdf.setMinuteSuffix(":");
458: rdf.setSecondSuffix("");
459: System.out.println(rdf.format(c1.getTime()));
460: System.out.println();
461: }
462: }