1 package tim.prune.data;
4 import java.text.DateFormat;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.TimeZone;
11 * Superclass of all timestamp implementations
13 public abstract class Timestamp
15 private static final DateFormat DEFAULT_DATE_FORMAT = DateFormat.getDateInstance();
16 private static final DateFormat DEFAULT_TIME_FORMAT = DateFormat.getTimeInstance();
18 protected static final DateFormat DEFAULT_DATETIME_FORMAT = DateFormat.getDateTimeInstance();
20 protected static final DateFormat ISO_8601_FORMAT
21 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
22 protected static final DateFormat ISO_8601_FORMAT_WITH_MILLIS
23 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
25 private static boolean _millisAddedToTimeFormat = false;
28 /** Possible formats for parsing and displaying timestamps */
37 // Static block to initialise date formats
40 // Set timezone for output
41 TimeZone gmtZone = TimeZone.getTimeZone("GMT");
42 ISO_8601_FORMAT.setTimeZone(gmtZone);
43 ISO_8601_FORMAT_WITH_MILLIS.setTimeZone(gmtZone);
44 DEFAULT_DATETIME_FORMAT.setTimeZone(gmtZone);
49 * @return true if valid
51 public abstract boolean isValid();
54 * Get a calendar representing this timestamp
56 public abstract Calendar getCalendar(TimeZone inZone);
59 * @return the milliseconds according to the given timezone
61 public abstract long getMilliseconds(TimeZone inZone);
64 * @return true if this timestamp is after the other one
66 public boolean isAfter(Timestamp inOther)
68 return getMillisecondsSince(inOther) > 0;
72 * @return true if this timestamp is before the other one
74 public boolean isBefore(Timestamp inOther)
76 return getMillisecondsSince(inOther) < 0;
80 * @return true if this timestamp is equal to the other one
82 public boolean isEqual(Timestamp inOther)
84 return getMillisecondsSince(inOther) == 0;
88 * @return the number of seconds since the other timestamp
90 public long getSecondsSince(Timestamp inOther)
92 return getMillisecondsSince(inOther) / 1000L;
96 * Calculate the difference between two Timestamps in milliseconds
97 * @param inOther other, earlier Timestamp
98 * @return number of milliseconds since other timestamp
100 public long getMillisecondsSince(Timestamp inOther)
102 return getMilliseconds(null) - inOther.getMilliseconds(null);
106 * @return the number of seconds since the other timestamp using the given timezone
108 public long getSecondsSince(Timestamp inOther, TimeZone inTimezone)
110 return (getMilliseconds(inTimezone) - inOther.getMilliseconds(inTimezone)) / 1000L;
114 * Add the given number of seconds offset
115 * @param inOffset number of seconds to add/subtract
117 public abstract void addOffsetSeconds(long inOffset);
120 * @return true if the timestamp has non-zero milliseconds
122 protected abstract boolean hasMilliseconds();
126 * @return date part of timestamp in locale-specific format
128 public String getDateText(TimeZone inTimezone)
130 if (!isValid()) return "";
131 return format(DEFAULT_DATE_FORMAT, inTimezone);
135 * @return Description of time part of timestamp in locale-specific format
137 public String getTimeText(TimeZone inTimezone)
139 if (!isValid()) return "";
140 // Maybe we should add milliseconds to this format?
141 if (hasMilliseconds() && !_millisAddedToTimeFormat)
145 SimpleDateFormat sdf = (SimpleDateFormat) DEFAULT_TIME_FORMAT;
146 String pattern = sdf.toPattern();
147 if (pattern.indexOf("ss") > 0 && pattern.indexOf("SS") < 0)
149 sdf.applyPattern(pattern.replaceFirst("s+", "$0.SSS"));
150 _millisAddedToTimeFormat = true;
153 catch (ClassCastException cce) {}
155 return format(DEFAULT_TIME_FORMAT, inTimezone);
159 * Utility method for formatting dates / times
161 protected abstract String format(DateFormat inFormat, TimeZone inTimezone);
165 * @return Description of timestamp in locale-specific format
167 public String getText(TimeZone inTimezone)
169 return getText(Format.LOCALE, inTimezone);
173 * @param inFormat format of timestamp
174 * @return Description of timestamp in required format
176 public String getText(Format inFormat, TimeZone inTimezone)
186 return format(DEFAULT_DATETIME_FORMAT, inTimezone);
188 return format(hasMilliseconds() ? ISO_8601_FORMAT_WITH_MILLIS : ISO_8601_FORMAT,