The printf method in Java is used for formatted output.

Formatting specifies the form of output, including precision of numbers, character width, date formats, and so on. Below are some common format specifiers and their usage:

String formatting

  • %s : output string

Example

System.out.printf("%s", "hello world"); // output "hello world"

Integer formatting

  • %d : output decimal integer
  • %o : output octal integer
  • %x : output hexadecimal integer
  • %X : output uppercase hexadecimal integer

Example

System.out.printf("%d %o %x %X", 10, 10, 10, 10); // output "10 12 a A"

Floating-point formatting

  • %f : output floating-point number
  • %e : output floating-point number in scientific notation (lowercase e)
  • %E : output floating-point number in scientific notation (uppercase E)
  • %.nf : control the number of digits after the decimal point, n is a number

Example

System.out.printf("%.2f %e %E", 3.1415926, 3.1415926, 3.1415926); // output "3.14 3.141593e+00 3.141593E+00"

Boolean formatting

  • %b : output boolean value

Example

System.out.printf("%b", true); // output "true"

Character formatting

  • %c : output character

Example

System.out.printf("%c", 'a'); // output "a"

Width and precision formatting

  • %n$ : specify the nth parameter
  • %m.n : control width and precision, m is the minimum width, n is the number of digits after the decimal point

Example

System.out.printf("%2d %5.2f %s", 10, 3.1415926, "hello"); // output "10 3.14 hello"

Below is a complete list of format specifiers available in the printf method in Java:

  • %d: signed decimal integer
  • %o: unsigned octal integer
  • %x, %X: unsigned hexadecimal integer, represented using lowercase and uppercase letters respectively
  • %f, %F: decimal floating-point number with decimal point, represented using lowercase and uppercase letters respectively
  • %e, %E: floating-point number with exponent, represented using lowercase and uppercase letters respectively
  • %g, %G: automatically chooses %f or %e format based on the magnitude of the value, represented using lowercase and uppercase letters respectively
  • %a, %A: hexadecimal floating-point number, represented using lowercase and uppercase letters respectively
  • %c: character type, outputs a single character
  • %s: string type, outputs a string
  • %n: newline character
  • %%: outputs a percent sign %
  • %h, %H: hash code, represented in hexadecimal and uppercase hexadecimal respectively
  • %t, %T: date and time formatting, using specific conversion characters, e.g., %tY represents the year, %tB represents the full month name
  • %b, %B: boolean type, outputs true or false, represented using lowercase and uppercase letters respectively
  • %S: outputs the string in uppercase letters

Below is a list of symbols in the Java printf method that can be used to format dates:

  • %tB: localized full month name
  • %tb: localized abbreviated month name
  • %tA: localized full weekday name
  • %ta: localized abbreviated weekday name
  • %tY: four-digit year
  • %ty: two-digit year
  • %tm: two-digit month
  • %td: two-digit day of the month
  • %te: one- or two-digit day of the month
  • %tH: hour in 24-hour format
  • %tI: hour in 12-hour format
  • %tM: minutes
  • %tS: seconds
  • %tp: morning or afternoon (AM/PM)
  • %tZ: time zone

Example

import java.util.Date; /** * Use printf for output * Key technical points * Use the printf method of java.io.PrintStream to implement C-style output * The first parameter of the printf method is the output format, and the second parameter is variable-length, representing the data objects to be output*/ /*** Output string ***/ public class Printf { public static void main(String[] args) { /*%s means output string, that is, replace %s in the pattern with the following string*/ //%n means newline System.out.printf("%s", new Integer(1212)); //It can also support multiple parameters System.out.printf("%s%n", "end line"); //%S outputs the string in uppercase System.out.printf("%s = %s%n", "Name", "Zhangsan"); //When multiple parameters are supported, you can insert argument indices between %s; 1$ indicates the first string, 3$ indicates the third string System.out.printf("%S = %s%n", "Name", "Zhangsan"); //** Output boolean type ** System.out.printf("%1$s = %3$s %2$s%n", "Name", "san", "Zhang"); /*** Output integer type ***/ System.out.printf("true = %b; false = ", true); System.out.printf("%b%n", false); /*%d means formatting the integer as a decimal integer*/ Integer iObj = 342; //%o means formatting the integer as an octal integer System.out.printf("%d; %d; %d%n", -500, 2343L, iObj); //%x means formatting the integer as a hexadecimal integer System.out.printf("%o; %o; %o%n", -500, 2343L, iObj); //%X means formatting the integer as a hexadecimal integer, with letters in uppercase System.out.printf("%x; %x; %x%n", -500, 2343L, iObj); //** Output floating-point type ** System.out.printf("%X; %X; %X%n", -500, 2343L, iObj); /*%e means output floating-point number in scientific notation*/ Double dObj = 45.6d; //%E means output floating-point number in scientific notation, in uppercase form System.out.printf("%e; %e; %e%n", -756.403f, 7464.232641d, dObj); //%f means formatting and outputting floating-point number in decimal System.out.printf("%E; %E; %E%n", -756.403f, 7464.232641d, dObj); //You can also limit the number of digits after the decimal point System.out.printf("%f; %f; %f%n", -756.403f, 7464.232641d, dObj); //** Output date type ** System.out.printf("%.1f; %.3f; %f%n", -756.403f, 7464.232641d, dObj); /*%t means formatting date and time type; %T is the uppercase form for date/time; after %t, a specific letter indicates different output formats*/ //Formatting year, month, day Date date = new Date(); long dataL = date.getTime(); //After %t, use y to output the year of the date (2-digit year, e.g., 99) //After %t, use m to output the month of the date; after %t, use d to output the day of the date //After %t, use Y to output the year of the date (4-digit year), System.out.printf("%1$ty-%1$tm-%1$td; %2$ty-%2$tm-%2$td%n", date, dataL); //After %t, use B to output the full name of the month of the date; after %t, use b to output the abbreviated name of the month of the date //The following are common date combinations System.out.printf("%1$tY-%1$tB-%1$td; %2$tY-%2$tb-%2$td%n", date, dataL); //After %t, use D to format the date as "%tm/%td/%ty" //After %t, use F to format the date as "%tY-%tm-%td" System.out.printf("%1$tD%n", date); //** Output time type ** System.out.printf("%1$tF%n", date); /*Output hours, minutes, seconds*/ //After %t, use H to output the hour of the time (24-hour); after %t, use I to output the hour of the time (12-hour), //After %t, use M to output the minutes of the time; after %t, use S to output the seconds of the time //After %t, use L to output the milliseconds of the seconds of the time System.out.printf("%1$tH:%1$tM:%1$tS; %2$tI:%2$tM:%2$tS%n", date, dataL); //After %t, use p to output the morning/afternoon (AM/PM) information of the time System.out.printf("%1$tH:%1$tM:%1$tS %1$tL%n", date); //The following are common time combinations System.out.printf("%1$tH:%1$tM:%1$tS %1$tL %1$tp%n", date); //After %t, use R to format the time as "%tH:%tM" //After %t, use T to format the time as "%tH:%tM:%tS" System.out.printf("%1$tR%n", date); //After %t, use r to format the time as "%tI:%tM:%tS %Tp" System.out.printf("%1$tT%n", date); //** Output day of the week ** System.out.printf("%1$tr%n", date); /*After %t, use A to get the full name of the day of the week*/ //After %t, use a to get the abbreviated name of the day of the week System.out.printf("%1$tF %1$tA%n", date); //Output complete date and time information System.out.printf("%1$tF %1$ta%n", date); //* * In the printf method, the format "%s" outputs the first argument value of the second variable-length parameter as a string; * The format "%n" means newline; the format "%S" outputs the string in uppercase; use "n$" between "%s" to * output the nth argument value of the variable-length parameter. The format "%b" outputs the first argument value * of the second variable-length parameter as a boolean value. System.out.printf("%1$tc%n", date); } } /** * The format "%d" outputs as a decimal integer; "%o" outputs as octal; "%x" outputs as hexadecimal; * "%X" outputs as hexadecimal, with letters (A, B, C, D, E, F) in uppercase. The format "%e" outputs * floating-point numbers in scientific notation; "%E" outputs floating-point numbers in scientific notation with e in uppercase; the format * "%f" outputs as a decimal floating-point number; adding ".n" between "%f" means retaining n digits after the decimal point when outputting.*/ /** * The format "%t" outputs date and time type. After "%t", use y to output the two-digit year of the date (e.g., 99), use m * to output the month of the date, use d to output the day of the date; after "%t", use Y to output the four-digit year of the date * (e.g., 1999), use B to output the full name of the month of the date, use b to output the abbreviated name of the month of the date. After "%t", use D * to output the date in the format "%tm/%td/%ty", and use F to output the date in the format "%tY-%tm-%td".*/ /** * Formattingis"%t"RepresentsOutputTimedayperiodType."%t"afteruseyRepresentsOutputdayperiodof二bitsnumberofYear(For example99)、usem * RepresentsOutputdayperiodofmonth份,usedRepresentsOutputdayperiodofdayNumber;"%t"afteruseYRepresentsOutputdayperiodoffourbitsnumberofYear * (For example1999)、useBRepresentsOutputdayperiodofmonth份ofCompletename,usebRepresentsOutputdayperiodofmonth份of简称."%t"afteruseD * indicates that"%tm/%td/%ty"ofFormattingOutputdayperiod、useFindicates that"%tY-%tm-%td"ofFormattingOutputdayperiod.*/ /** * After "%t", use H to represent the hour of the output time (24-hour clock), I to represent the hour of the output time (12-hour clock), M to represent the minute * of the output time, S to represent the second of the output time, L to represent the milliseconds within the second of the output time, and p to represent whether the output time is * AM or PM. After "%t", use R to output the time in the format "%tH:%tM", use T to output the time in the format "%tH:%tM:%tS", * and use r to output the time in the format "%tI:%tM:%tS %Tp".*/ /** * After "%t", use A to represent the full name of the output date, and use a to represent the abbreviated weekday name of the output date.*/