Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
2.0
-
None
-
None
-
Operating System: other
Platform: All
-
29794
Description
it is sometimes necessary to format based on a long timeInMillis [for instance
either directly from System.currentTimeInMillis or from a File.lastModified()].
I have an extension of FastDateFormat that adds the following methods to
support this:
/**
- <p>Formats a <code>long</code> timeInMillis. Useful if you don't
want - to create a Date or don't have a Calendar intance</p>
- @param date the new time in UTC milliseconds from the epoch
(1/1/1970). - @return the formatted string
*/
public String format(long timeInMillis) { return format(timeInMillis, new StringBuffer (mMaxLengthEstimate)).toString(); }
/**
- <p>Formats a <code>long</code> timeInMillis. Useful if you don't
want - to create a Date or don't have a Calendar intance</p>
- @param date the new time in UTC milliseconds from the epoch
(1/1/1970). - @return the formatted string
*/
public StringBuffer format(long timeInMillis, StringBuffer buf) {
class UtilGregorianCalendar extends GregorianCalendar {
UtilGregorianCalendar(TimeZone timeZone) { super(timeZone); }
public void setTimeInMillis(long timeInMillis)
{ super.setTimeInMillis(timeInMillis); }}
UtilGregorianCalendar c = new UtilGregorianCalendar(mTimeZone);
c.setTimeInMillis(timeInMillis);
return applyRules(c, buf);
}
and a minor change to format(Object, StringBuffer, FieldPosition:
else if (obj instanceof Long)
{ return format(((Long) obj).longValue(), toAppendTo); }