

Left-justifying within the specified width: String.format("|%-20d|", 93) // prints: |93 | With the %d format specifier, you can use an argument of all integral types including byte, short, int, long and BigInteger.ĭefault formatting: String.format("%d", 93) // prints 93 String.format("%2$s", 32, "Hello") // prints: "Hello" Formatting an Integer “ UTC“, “ PST“, etc.Īn argument index is specified as a number ending with a “ $” after the “ %” and selects the specified argument in the argument list. “60” is required to support leap seconds. Seconds within the minute formatted with 2 digits e.g. Milliseconds since epoch Jan 1, 1970 00:00:00 UTC. Nanosecond formatted with 9 digits and leading 0s e.g. Minute within the hour formatted a leading 0 e.g. Hour of the day for the 12-hour click without a leading 0 e.g. Hour of the day for the 24 hour clock without a leading 0 e.g. “ 01” – “ 12“.ĭay of the year formatted with leading 0s e.g. Hour of the day for the 12-hour clock e.g. Hour of the day for the 24-hour clock e.g.
#Java formatter iso#
ISO 8601 formatted date with “ %tY-%tm-%td“. “ 01” to “ 31“.ĭay of the month formatted without a leading 0 e.g. “00” through “99”.ĭate and time formatted with “ %ta %tb %td %tT %tZ %tY”ĭay of the month formatted with two digits. “ Jan“, “ Feb“, etc.Ĭentury part of year formatted with two digits e.g.

“ January“, “ February“, etc.Ībbreviated month name e.g. “ Sun“, “ Mon“, etc.įull name of the month e.g. “ Sunday“, “ Monday“Ībbreviated name of the week day e.g. Note: Using the formatting characters with “%T” instead of “%t” in the table below makes the output uppercase.įull name of the day of the week, e.g. More formatting flags are needed after this. %t is the prefix for Date/Time conversions. long, Calendar, Date and TemporalAccessor) Hex String of value from hashCode() method.ĭate/Time (incl.

byte, short, int, long, bigint)ĭecimal number, possibly in scientific notation depending on the precision and value. Here is a quick reference to all the conversion specifiers supported: you can continue to append data to sbuf here. StringBuilder sbuf = new StringBuilder() Output formatted using the format() method will be appended to the StringBuilder. ("My name is: %s%n", "joe") Ĭreate a Formatter and link it to a StringBuilder. String output = String.format("%s = %d", "joe", 35) įor formatted console output, you can use printf() or the format() method of System.out and System.err PrintStreams.

If there were a “java sprintf” then this would be it. The most common way of formatting a string in java is using String.format(). You may also want to take a look at What's New in Java 8 from Pluralsight or 38 Java String-Related Techniques Making Your Life Easier, a course on Udemy. This guide is an attempt to bring some clarity and ease the usage of string formatting in Java. While it does include all the information, the organization leaves something to be desired. Have you tried to read and understand Java’s String format documentation? I have and found it nearly impenetrable. When you purchase through links on our site, we may receive an affiliate commission.
