1 Answer

0 votes
by

String Formatting

The most common way of formatting a string in java is using String.format(). If there were a “java sprintf” then this would be it.

String output = String.format("%s = %d", "joe", 35);

For formatted console output, you can use printf() or the format() method of System.out and System.err PrintStreams.

System.out.printf("My name is: %s%n", "joe");

Create a Formatter and link it to a StringBuilder. Output formatted using the format() method will be appended to the StringBuilder.

StringBuilder sbuf = new StringBuilder();

Formatter fmt = new Formatter(sbuf);

fmt.format("PI = %f%n", Math.PI);

System.out.print(sbuf.toString());

// you can continue to append data to sbuf here.

Format Specifiers

Here is a quick reference to all the conversion specifiers supported:

SPECIFIER APPLIES TO OUTPUT

%a floating point (except BigDecimal) Hex output of floating point number

%f floating point decimal number

%g floating point decimal number, possibly in scientific notation depending on the precision and value.

%b Any type “true” if non-null, “false” if null

%c character Unicode character

%n none Platform-specific line separator.

%o integer (incl. byte, short, int, long, bigint) Octal number

%s any type String value

%t Date/Time (incl. long, Calendar, Date and TemporalAccessor)

%t      is the prefix for Date/Time conversions. More formatting flags are needed after this. 

%d integer (incl. byte, short, int, long, bigint) Decimal Integer

%e floating point decimal number in scientific notation

%h any type Hex String of value from hashCode() method.

%x integer (incl. byte, short, int, long, bigint)

Hex string.

Date and Time Formatting

Note: Using the formatting characters with “%T” instead of “%t” in the table below makes the output uppercase.

 FLAG NOTES

%td Day of the month formatted with two digits. e.g. “01” to “31“.

%tc Date and time formatted with “%ta %tb %td %tT %tZ %tY” e.g. “Fri Feb 17 07:45:42 PST 2017“

%tD Date formatted as “%tm/%td/%ty“

%te Day of the month formatted without a leading 0 e.g. “1” to “31”.

%tF ISO 8601 formatted date with “%tY-%tm-%td“.

%tH Hour of the day for the 24-hour clock e.g. “00” to “23“.

%th Same as %tb.

%tI Hour of the day for the 12-hour clock e.g. “01” – “12“.

%tj Day of the year formatted with leading 0s e.g. “001” to “366“.

%tk Hour of the day for the 24 hour clock without a leading 0 e.g. “0” to “23“.

%tl Hour of the day for the 12-hour click without a leading 0 e.g. “1” to “12“.

%tM Minute within the hour formatted a leading 0 e.g. “00” to “59“.

%tm Month formatted with a leading 0 e.g. “01” to “12“.

%tN Nanosecond formatted with 9 digits and leading 0s e.g. “000000000” to “999999999”.

%tp Locale specific “am” or “pm” marker.

%tA Full name of the day of the week, e.g. “Sunday“, “Monday“

%ta Abbreviated name of the week day e.g. “Sun“, “Mon“, etc.

%tB Full name of the month e.g. “January“, “February“, etc.

%tb Abbreviated month name e.g. “Jan“, “Feb“, etc.

%tC Century part of year formatted with two digits e.g. “00” through “99”.

%tQ Milliseconds since epoch Jan 1 , 1970 00:00:00 UTC.

%tR Time formatted as 24-hours e.g. “%tH:%tM“.

%tr Time formatted as 12-hours e.g. “%tI:%tM:%tS %Tp“.

%tS Seconds within the minute formatted with 2 digits e.g. “00” to “60”. “60” is required to support leap seconds.

%ts Seconds since the epoch Jan

Related questions

0 votes
    Me and my colleague have different versions of VisualStudio. He used interpolated string and I couldn't ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    So I've been reading this tutorial on how to use Frida: https://www.frida.re/docs/functions/ and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    So I've been reading this tutorial on how to use Frida: https://www.frida.re/docs/functions/ and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a table which contains 'NULL' values which are of type 'Datetime'. Now i have to convert ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I am trying to convert a string into date format in Google Sheet. I've tried different formulas but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I think the direct answer to the question is 'No' but I'm hoping that someone has written a real ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I want' to parse dates in this format, but ignore parts of the string. 'Wed, 27 Oct 1770 22:17: ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I have a DropDownList with values that I get from a SQL database. Depending on the selection made from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    I have a DropDownList with values that I get from a SQL database. Depending on the selection made from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    y = " \{ Hi\} {0} " print y.format(43) it's showing an error: Key Error: Hi\\ I want to print the output from the above syntax: {Hi} 43 Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    In Python 2.6, the str.format() has been introduced and has a slightly different syntax than existing % operator. ... %s" % some_info) Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: converting date format (5 answers) Closed 5 years ago. I have a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am working in an graphql application where I have to send custom error object / message in json ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    Does anyone know how to solve this java error? java.io.IOException: Invalid keystore format I get it when ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Does anyone know how to solve this java error? java.io.IOException: Invalid keystore format I get it when ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
...