You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2004/06/25 04:37:15 UTC

DO NOT REPLY [Bug 29794] New: - Add convenience format(long) methods to FastDateFormat

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=29794>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=29794

Add convenience format(long) methods to FastDateFormat

           Summary: Add convenience format(long) methods to FastDateFormat
           Product: Commons
           Version: 2.0 Final
          Platform: All
        OS/Version: Other
            Status: NEW
          Severity: Enhancement
          Priority: Other
         Component: Lang
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: jed.wesley-smith@combined.com.au


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);
		}

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org