Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-1659

Unnecessary OffsetDateTime creation in CachingDateFormat

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Major Major
    • 1.3.0
    • 1.3.0-beta0
    • logback-core
    • None

    Description

      CachingDateFormat creates a temporary OffsetDateTime from the given instant to apply the configured time zone (see here) before passing it to the DateTimeFormatter for rendering.

      This temporary OffsetDateTime can be avoided by setting the ZoneId directly on the DateTimeFormatter when it is initialized. This is apparently what was foreseen in the constructor with the following lines:

      public CachingDateFormatter(String pattern, ZoneId aZoneId) {
         dtf = DateTimeFormatter.ofPattern(pattern);
         ...
         dtf.withZone(this.zoneId);
         ...
      }

      Unfortunately this won't work because "withZone()" returns a new formatter which is not assigned to the "dtf" instance variable...

       

      To summarise:

      (1) In the constructor do something like:

      dtf = DateTimeFormatter.ofPattern(pattern).withZone(this.zoneId)
      

      (2) In the "format()" method do something like:

      Instant instant = Instant.ofEpochMilli(now);
      String result = dtf.format(instant);
      

      --> no need for an intermediate OffsetDateTime anymore

      Attachments

        Activity

          People

            ceki Ceki Gülcü
            brenuart Bertrand Renuart
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: