Commons Lang
  1. Commons Lang
  2. LANG-222

[lang] Add convenience format(long) methods to FastDateFormat

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0
    • Fix Version/s: 2.1
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: All

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

        Activity

        Jed Wesley-Smith created issue -
        Hide
        Henri Yandell added a comment -

        The UtilGregorianCalendar is a bit of a worry, if only for performance. Calendar
        creation sucks up time, so we'd want to cache this.

        Show
        Henri Yandell added a comment - The UtilGregorianCalendar is a bit of a worry, if only for performance. Calendar creation sucks up time, so we'd want to cache this.
        Hide
        Jed Wesley-Smith added a comment -

        while it would probably be desirable to do so, there are a whole lot of
        synchronization issues to deal with to do so that would affect scalability.
        Also, the same improvement could be made of format(Date, Locale), as a new
        Calendar is constructed for each of these calls, and even format(Calendar ...)
        when timezone forced makes a clone for each call.

        I am not particularly attached to the implementation though, and as long as it
        is thread-safe and scales fairly linearly I am happy.

        Looking at the code though, I think perhaps an explanatory jdoc on the public
        setTimeInMillis(long) should be added for clarity (even though it should be
        fairly obvious)

        /**

        • {@link java.util.Calendar#setTimeInMillis(long)}

          is protected, make it public
          */
          public void setTimeInMillis(long timeInMillis)

          {...}
        Show
        Jed Wesley-Smith added a comment - while it would probably be desirable to do so, there are a whole lot of synchronization issues to deal with to do so that would affect scalability. Also, the same improvement could be made of format(Date, Locale), as a new Calendar is constructed for each of these calls, and even format(Calendar ...) when timezone forced makes a clone for each call. I am not particularly attached to the implementation though, and as long as it is thread-safe and scales fairly linearly I am happy. Looking at the code though, I think perhaps an explanatory jdoc on the public setTimeInMillis(long) should be added for clarity (even though it should be fairly obvious) /** {@link java.util.Calendar#setTimeInMillis(long)} is protected, make it public */ public void setTimeInMillis(long timeInMillis) {...}
        Hide
        Stephen Colebourne added a comment -

        Code adde that simply creates a Date object and calls on. Creating a Date
        object is relatively inexpensive, and does not create a GregCalendar.

        Show
        Stephen Colebourne added a comment - Code adde that simply creates a Date object and calls on. Creating a Date object is relatively inexpensive, and does not create a GregCalendar.
        Hide
        Jed Wesley-Smith added a comment -

        cheers, appreciated. btw. final statement is not exactly correct as the format
        (Date) methods create a Calendar and call on to format(Calendar). As I was
        trying to avoid this extra Object creation I came up with the proposed
        implementation rather than the much simpler one as implemented. I should have
        performance tested though, as there is no significant difference between the
        simple and complex implementations that I could see under Java 1.3 or 1.4. Do
        you want me to verify or close it?

        Show
        Jed Wesley-Smith added a comment - cheers, appreciated. btw. final statement is not exactly correct as the format (Date) methods create a Calendar and call on to format(Calendar). As I was trying to avoid this extra Object creation I came up with the proposed implementation rather than the much simpler one as implemented. I should have performance tested though, as there is no significant difference between the simple and complex implementations that I could see under Java 1.3 or 1.4. Do you want me to verify or close it?
        Hide
        Stephen Colebourne added a comment -

        If you are happy, a verified is nice. Thanks.

        Show
        Stephen Colebourne added a comment - If you are happy, a verified is nice. Thanks.
        Hide
        Jed Wesley-Smith added a comment -

        tested implementation, couldn't find a way to make it any faster. good stuff!

        Show
        Jed Wesley-Smith added a comment - tested implementation, couldn't find a way to make it any faster. good stuff!
        Hide
        Henri Yandell added a comment -

        2.1 released, closing.

        Show
        Henri Yandell added a comment - 2.1 released, closing.
        Henri Yandell made changes -
        Field Original Value New Value
        issue.field.bugzillaimportkey 29794 12341539
        Henri Yandell made changes -
        Assignee Jakarta Commons Developers Mailing List [ commons-dev@jakarta.apache.org ]
        Component/s Lang [ 12311121 ]
        Fix Version/s 2.1 Final [ 12311659 ]
        Affects Version/s 2.0 Final [ 12311658 ]
        Key COM-1387 LANG-222
        Project Commons [ 12310458 ] Commons Lang [ 12310481 ]
        Henri Yandell made changes -
        Affects Version/s 2.0 Final [ 12311706 ]
        Henri Yandell made changes -
        Fix Version/s 2.1 Final [ 12311701 ]
        Mark Thomas made changes -
        Workflow jira [ 12370444 ] Default workflow, editable Closed status [ 12602410 ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Jed Wesley-Smith
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development