Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8336741

Optimize LocalTime.toString with StringBuilder.repeat

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 24
    • None
    • core-libs
    • b08

      class LocalTime {
          public String toString() {
              // ...
                      if (nanoValue % 1000_000 == 0) {
                          buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
                      } else if (nanoValue % 1000 == 0) {
                          buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
                      } else {
                          buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
                      }
             // ...
          }
      }
      Currently, LocalTime.toString handles nanos by adding a value and then subString(1) to fill it with zeros. Using StringBuilder.repeat is more concise and has better performance.

            swen Shaojin Wen
            swen Shaojin Wen
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: