Java Formatting Output printf Example
Example
import java.util.Date;
/**
* Using printf for output
*/
/** Key Technical Points
* Using the printf method of java.io.PrintStream to achieve C-style output
* The first parameter of the printf method is the output format, the second parameter is variable-length, representing the data objects to be output
*/
public class Printf {
public static void main(String[] args) {
/*** Output String ***/
// %s indicates outputting a string, which replaces the %s in the pattern with the subsequent string
System.out.printf("%s", new Integer(1212));
// %n indicates a newline
System.out.printf("%s%n", "end line");
// Supports multiple parameters
System.out.printf("%s = %s%n", "Name", "Zhangsan");
// %S outputs the string in uppercase
System.out.printf("%S = %s%n", "Name", "Zhangsan");
// When supporting multiple parameters, variable numbers can be inserted between %s, 1$ indicates the first string, 3$ indicates the third string
System.out.printf("%1$s = %3$s %2$s%n", "Name", "san", "Zhang");
/*** Output boolean type ***/
System.out.printf("true = %b; false = ", true);
System.out.printf("%b%n", false);
/*** Output integer type ***/
Integer iObj = 342;
// %d formats the integer as a decimal integer
System.out.printf("%d; %d; %d%n", -500, 2343L, iObj);
// %o formats the integer as an octal integer
System.out.printf("%o; %o; %o%n", -500, 2343L, iObj);
// %x formats the integer as a hexadecimal integer
System.out.printf("%x; %x; %x%n", -500, 2343L, iObj);
// %X formats the integer as a hexadecimal integer, with uppercase letters
System.out.printf("%X; %X; %X%n", -500, 2343L, iObj);
/*** Output floating-point type ***/
Double dObj = 45.6d;
// %e outputs the floating-point number in scientific notation
System.out.printf("%e; %e; %e%n", -756.403f, 7464.232641d, dObj);
// %E outputs the floating-point number in scientific notation, with uppercase E
System.out.printf("%E; %E; %E%n", -756.403f, 7464.232641d, dObj);
// %f outputs the floating-point number in decimal format
System.out.printf("%f; %f; %f%n", -756.403f, 7464.232641d, dObj);
// Can also limit the number of decimal places
System.out.printf("%.1f; %.3f; %f%n", -756.403f, 7464.232641d, dObj);
/*** Output date type ***/
// %t is used to format date and time types, %T is the uppercase form of date and time, followed by specific letters for different output formats
Date date = new Date();
long dataL = date.getTime();
// Format year-month-day
// %t followed by y for the year in 2 digits, m for the month, d for the day
System.out.printf("%1$ty-%1$tm-%1$td; %2$ty-%2$tm-%2$td%n", date, dataL);
// %t followed by Y for the year in 4 digits,
// %t followed by B for the full month name, b for the abbreviated month name
System.out.printf("%1$tY-%1$tB-%1$td; %2$tY-%2$tb-%2$td%n", date, dataL);
// Common date combinations
// %t followed by D to format the date as "%tm/%td/%ty"
System.out.printf("%1$tD%n", date);
// %t followed by F to format the date as "%tY-%tm-%td"
System.out.printf("%1$tF%n", date);
/*** Output time type ***/
// Output hour-minute-second
// %t followed by H for the hour in 24-hour format, I for the hour in 12-hour format,
// %t followed by M for minutes, S for seconds
System.out.printf("%1$tH:%1$tM:%1$tS; %2$tI:%2$tM:%2$tS%n", date, dataL);
// %t followed by L for milliseconds in the second
System.out.printf("%1$tH:%1$tM:%1$tS %1$tL%n", date);
// %t followed by p for AM/PM information
System.out.printf("%1$tH:%1$tM:%1$tS %1$tL %1$tp%n", date);
// Common time combinations
// %t followed by R to format the time as "%tH:%tM"
System.out.printf("%1$tR%n", date);
// %t followed by T to format the time as "%tH:%tM:%tS"
System.out.printf("%1$tT%n", date);
// %t followed by r to format the time as "%tI:%tM:%tS %Tp"
System.out.printf("%1$tr%n", date);
/*** Output day of the week ***/
// %t followed by A for the full name of the day of the week
System.out.printf("%1$tF %1$tA%n", date);
// %t followed by a for the abbreviated name of the day of the week
System.out.printf("%1$tF %1$ta%n", date);
// Output full date and time information
System.out.printf("%1$tc%n", date);
}
}
/**
* In the printf method, the format "%s" indicates outputting the first parameter of the second variable-length parameter as a string;
* The format "%n" indicates a newline; the format "%S" indicates outputting the string in uppercase; between "%s", "n$" indicates
* outputting the nth parameter of the variable-length parameter. The format "%b" indicates outputting the first parameter of the second variable-length parameter
* as a boolean value.
*/
/**
* The format "%d" indicates outputting in decimal integer format; "%o" indicates outputting in octal format; "%x" indicates outputting in hexadecimal
* format; "%X" indicates outputting in hexadecimal format, with uppercase letters (A, B, C, D, E, F). The format "%e" indicates
* outputting floating-point numbers in scientific notation; the format "%E" indicates outputting floating-point numbers in scientific notation, with the letter e in uppercase; the format
* "%f" indicates outputting in decimal floating-point format, adding ".n" between "%f" to retain n decimal places.
*/
/**
* The format "%t" indicates outputting date and time types. "%t" followed by y for the year in 2 digits, m
* for the month, d for the day; "%t" followed by Y for the year in 4 digits,
* B for the full month name, b for the abbreviated month name. "%t" followed by D
* to format the date as "%tm/%td/%ty", F to format the date as "%tY-%tm-%td".
*/
/**
* "%t" followed by H for the hour in 24-hour format, I for the hour in 12-hour format, M for minutes,
* S for seconds, L for milliseconds in the second, p for AM/PM information. "%t" followed by R to format the time as "%tH:%tM", T to format the time as "%tH:%tM:%tS",
* r to format the time as "%tI:%tM:%tS %Tp".
*/
/**
* "%t" followed by A for the full name of the day of the week, a for the abbreviated name of the day of the week.
*/