<div class="__aliyun_email_body_block"><div  style="line-height:1.7;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#0070c0;"><div  style="clear:both;"><br ></div><div  style="clear:both;"><span >I suggest that StringBuilder and StringBuffer add a method append(int i, int width, char pad),</span><div  style="clear:both;">StringBuilder add method append(int i, int width, char pad). As follows:</div><div  style="clear:both;">```java</div><div  style="clear:both;">public abstract class AbstractBuilder {</div><div  style="clear:both;">     public AbstractStringBuilder append(int i, int width, char pad) {</div><div  style="clear:both;">         // ...</div><div  style="clear:both;">     }</div><div  style="clear:both;">}</div><div  style="clear:both;">```</div><div  style="clear:both;"><br ></div><div  style="clear:both;">Such functionality is needed in many places in the JDK. In the JDK date processing code, there are many places where StringBuilder.append(int) is used to specify the width. </div><div  style="clear:both;">For example, in CalendarUtils, it is like this:</div><div  style="clear:both;">```java</div><div  style="clear:both;">package sun.util.calendar;</div><div  style="clear:both;">public final class CalendarUtils {</div><div  style="clear:both;">    /**</div><div  style="clear:both;">     * Mimics sprintf(buf, "%0*d", decaimal, width).</div><div  style="clear:both;">     */</div><div  style="clear:both;">    public static StringBuilder sprintf0d(StringBuilder sb, int value, int width) {</div><div  style="clear:both;">        long d = value;</div><div  style="clear:both;">        if (d < 0) {</div><div  style="clear:both;">            sb.append('-');</div><div  style="clear:both;">            d = -d;</div><div  style="clear:both;">            --width;</div><div  style="clear:both;">        }</div><div  style="clear:both;">        int n = 10;</div><div  style="clear:both;">        for (int i = 2; i < width; i++) {</div><div  style="clear:both;">            n *= 10;</div><div  style="clear:both;">        }</div><div  style="clear:both;">        for (int i = 1; i < width && d < n; i++) {</div><div  style="clear:both;">            sb.append('0');</div><div  style="clear:both;">            n /= 10;</div><div  style="clear:both;">        }</div><div  style="clear:both;">        sb.append(d);</div><div  style="clear:both;">        return sb;</div><div  style="clear:both;">    }</div><div  style="clear:both;">    public static StringBuffer sprintf0d(StringBuffer sb, int value, int width) {</div><div  style="clear:both;">        long d = value;</div><div  style="clear:both;">        if (d < 0) {</div><div  style="clear:both;">            sb.append('-');</div><div  style="clear:both;">            d = -d;</div><div  style="clear:both;">            --width;</div><div  style="clear:both;">        }</div><div  style="clear:both;">        int n = 10;</div><div  style="clear:both;">        for (int i = 2; i < width; i++) {</div><div  style="clear:both;">            n *= 10;</div><div  style="clear:both;">        }</div><div  style="clear:both;">        for (int i = 1; i < width && d < n; i++) {</div><div  style="clear:both;">            sb.append('0');</div><div  style="clear:both;">            n /= 10;</div><div  style="clear:both;">        }</div><div  style="clear:both;">        sb.append(d);</div><div  style="clear:both;">        return sb;</div><div  style="clear:both;">    }</div><div  style="clear:both;">}</div><div  style="clear:both;">```</div><div  style="clear:both;"><br ></div><div  style="clear:both;">This is the case in LocalTime. To fill in '0', first add 1000 or 1000_000 or 1000_000_000, and then do substring(1) processing after append. Such code has poor readability and poor performance.</div><div  style="clear:both;">```java</div><div  style="clear:both;">package java.time;</div><div  style="clear:both;">public final class LocalTime {</div><div  style="clear:both;">    public String toString() {</div><div  style="clear:both;">        StringBuilder buf = new StringBuilder(18);</div><div  style="clear:both;">        int hourValue = hour;</div><div  style="clear:both;">        int minuteValue = minute;</div><div  style="clear:both;">        int secondValue = second;</div><div  style="clear:both;">        int nanoValue = nano;</div><div  style="clear:both;">        buf.append(hourValue < 10 ? "0" : "").append(hourValue)</div><div  style="clear:both;">            .append(minuteValue < 10 ? ":0" : ":").append(minuteValue);</div><div  style="clear:both;">        if (secondValue > 0 || nanoValue > 0) {</div><div  style="clear:both;">            buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);</div><div  style="clear:both;">            if (nanoValue > 0) {</div><div  style="clear:both;">                buf.append('.');</div><div  style="clear:both;">                if (nanoValue % 1000_000 == 0) {</div><div  style="clear:both;">                    buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));</div><div  style="clear:both;">                } else if (nanoValue % 1000 == 0) {</div><div  style="clear:both;">                    buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));</div><div  style="clear:both;">                } else {</div><div  style="clear:both;">                    buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));</div><div  style="clear:both;">                }</div><div  style="clear:both;">            }</div><div  style="clear:both;">        }</div><div  style="clear:both;">        return buf.toString();</div><div  style="clear:both;">    }</div><span >}</span></div><div  style="clear:both;">```</div><div  style="clear:both;"><br ></div><div  style="clear:both;"><span >I submitted a PR ( <a  href="https://github.com/openjdk/jdk/pull/15993" target="_blank">https://github.com/openjdk/jdk/pull/15993</a> ), including the code and tests for the new method. </span><span >Please review and don't hesitate to critique my approach and patch.</span></div><div  style="clear:both;"><span ><br ></span></div></div></div>