1:
42: package ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53:
59: public class RelativeDateFormat extends DateFormat {
60:
61:
62: private long baseMillis;
63:
64:
67: private boolean showZeroDays;
68:
69:
73: private NumberFormat dayFormatter;
74:
75:
78: private String daySuffix;
79:
80:
83: private String hourSuffix;
84:
85:
88: private String minuteSuffix;
89:
90:
93: private NumberFormat secondFormatter;
94:
95:
98: private String secondSuffix;
99:
100:
103: private static long MILLISECONDS_IN_ONE_HOUR = 60 * 60 * 1000L;
104:
105:
108: private static long MILLISECONDS_IN_ONE_DAY = 24 * MILLISECONDS_IN_ONE_HOUR;
109:
110:
113: public RelativeDateFormat() {
114: this(0L);
115: }
116:
117:
122: public RelativeDateFormat(Date time) {
123: this(time.getTime());
124: }
125:
126:
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: this.calendar = new GregorianCalendar();
147: this.numberFormat = new DecimalFormat("0");
148: }
149:
150:
158: public long getBaseMillis() {
159: return this.baseMillis;
160: }
161:
162:
171: public void setBaseMillis(long baseMillis) {
172: this.baseMillis = baseMillis;
173: }
174:
175:
183: public boolean getShowZeroDays() {
184: return this.showZeroDays;
185: }
186:
187:
195: public void setShowZeroDays(boolean show) {
196: this.showZeroDays = show;
197: }
198:
199:
206: public String getDaySuffix() {
207: return this.daySuffix;
208: }
209:
210:
217: public void setDaySuffix(String suffix) {
218: if (suffix == null) {
219: throw new IllegalArgumentException("Null 'suffix' argument.");
220: }
221: this.daySuffix = suffix;
222: }
223:
224:
231: public String getHourSuffix() {
232: return this.hourSuffix;
233: }
234:
235:
242: public void setHourSuffix(String suffix) {
243: if (suffix == null) {
244: throw new IllegalArgumentException("Null 'suffix' argument.");
245: }
246: this.hourSuffix = suffix;
247: }
248:
249:
256: public String getMinuteSuffix() {
257: return this.minuteSuffix;
258: }
259:
260:
267: public void setMinuteSuffix(String suffix) {
268: if (suffix == null) {
269: throw new IllegalArgumentException("Null 'suffix' argument.");
270: }
271: this.minuteSuffix = suffix;
272: }
273:
274:
281: public String getSecondSuffix() {
282: return this.secondSuffix;
283: }
284:
285:
292: public void setSecondSuffix(String suffix) {
293: if (suffix == null) {
294: throw new IllegalArgumentException("Null 'suffix' argument.");
295: }
296: this.secondSuffix = suffix;
297: }
298:
299:
304: public void setSecondFormatter(NumberFormat formatter) {
305: if (formatter == null) {
306: throw new IllegalArgumentException("Null 'formatter' argument.");
307: }
308: this.secondFormatter = formatter;
309: }
310:
311:
321: public StringBuffer format(Date date, StringBuffer toAppendTo,
322: FieldPosition fieldPosition) {
323: long currentMillis = date.getTime();
324: long elapsed = currentMillis - this.baseMillis;
325:
326: long days = elapsed / MILLISECONDS_IN_ONE_DAY;
327: elapsed = elapsed - (days * MILLISECONDS_IN_ONE_DAY);
328: long hours = elapsed / MILLISECONDS_IN_ONE_HOUR;
329: elapsed = elapsed - (hours * MILLISECONDS_IN_ONE_HOUR);
330: long minutes = elapsed / 60000L;
331: elapsed = elapsed - (minutes * 60000L);
332: double seconds = elapsed / 1000.0;
333: if (days != 0 || this.showZeroDays) {
334: toAppendTo.append(this.dayFormatter.format(days) + getDaySuffix());
335: }
336: toAppendTo.append(String.valueOf(hours) + getHourSuffix());
337: toAppendTo.append(String.valueOf(minutes) + getMinuteSuffix());
338: toAppendTo.append(this.secondFormatter.format(seconds)
339: + getSecondSuffix());
340: return toAppendTo;
341: }
342:
343:
351: public Date parse(String source, ParsePosition pos) {
352: return null;
353: }
354:
355:
362: public boolean equals(Object obj) {
363: if (obj == this) {
364: return true;
365: }
366: if (!(obj instanceof RelativeDateFormat)) {
367: return false;
368: }
369: if (!super.equals(obj)) {
370: return false;
371: }
372: RelativeDateFormat that = (RelativeDateFormat) obj;
373: if (this.baseMillis != that.baseMillis) {
374: return false;
375: }
376: if (this.showZeroDays != that.showZeroDays) {
377: return false;
378: }
379: if (!this.daySuffix.equals(that.daySuffix)) {
380: return false;
381: }
382: if (!this.hourSuffix.equals(that.hourSuffix)) {
383: return false;
384: }
385: if (!this.minuteSuffix.equals(that.minuteSuffix)) {
386: return false;
387: }
388: if (!this.secondSuffix.equals(that.secondSuffix)) {
389: return false;
390: }
391: if (!this.secondFormatter.equals(that.secondFormatter)) {
392: return false;
393: }
394: return true;
395: }
396:
397:
402: public int hashCode() {
403: int result = 193;
404: result = 37 * result
405: + (int) (this.baseMillis ^ (this.baseMillis >>> 32));
406: result = 37 * result + this.daySuffix.hashCode();
407: result = 37 * result + this.hourSuffix.hashCode();
408: result = 37 * result + this.minuteSuffix.hashCode();
409: result = 37 * result + this.secondSuffix.hashCode();
410: result = 37 * result + this.secondFormatter.hashCode();
411: return result;
412: }
413:
414:
419: public Object clone() {
420: RelativeDateFormat clone = (RelativeDateFormat) super.clone();
421: clone.dayFormatter = (NumberFormat) this.dayFormatter.clone();
422: clone.secondFormatter = (NumberFormat) this.secondFormatter.clone();
423: return clone;
424: }
425:
426:
431: public static void main(String[] args) {
432: GregorianCalendar c0 = new GregorianCalendar(2006, 10, 1, 0, 0, 0);
433: GregorianCalendar c1 = new GregorianCalendar(2006, 10, 1, 11, 37, 43);
434: c1.set(Calendar.MILLISECOND, 123);
435:
436: System.out.println("Default: ");
437: RelativeDateFormat rdf = new RelativeDateFormat(c0.getTimeInMillis());
438: System.out.println(rdf.format(c1.getTime()));
439: System.out.println();
440:
441: System.out.println("Hide milliseconds: ");
442: rdf.setSecondFormatter(new DecimalFormat("0"));
443: System.out.println(rdf.format(c1.getTime()));
444: System.out.println();
445:
446: System.out.println("Show zero day output: ");
447: rdf.setShowZeroDays(true);
448: System.out.println(rdf.format(c1.getTime()));
449: System.out.println();
450:
451: System.out.println("Alternative suffixes: ");
452: rdf.setShowZeroDays(false);
453: rdf.setDaySuffix(":");
454: rdf.setHourSuffix(":");
455: rdf.setMinuteSuffix(":");
456: rdf.setSecondSuffix("");
457: System.out.println(rdf.format(c1.getTime()));
458: System.out.println();
459: }
460: }