string.format Usage: 1. Use the local language environment for the new string, formulate the string format and parameters to generate a formatted new string; 2. Use the specified language environment, formulate the string format and parameter generation Formatted string.
Conversion symbols | Detailed description | Example |
---|---|---|
String type | "Like Please bookmark" | |
Character type | 'm' | |
Boolean type | true | ##%d |
88 | %x | |
FF | ##%o | |
77 | ##%f | Floating point type |
% a | Hexadecimal floating point type | |
##%e | Exponential type | |
%g | General floating point type (the shorter of f and e types) | |
%h | Hash code | |
%% | Percent type | |
##%n | Line break | No examples (basically not used) |
%tx | Date and time types (x represents different date and time conversion characters) | No examples (basically not used) |
For the convenience of understanding, let’s give an example | Output results |
Result | |||
---|---|---|---|
15 | 0 | Add 0 in front of the number (commonly used for encryption) | |
0099 | Spaces | Adds the specified number of spaces before the integer | |
99 | ##, | Use "," to group numbers (commonly used to display the amount) | ("%,f", 9999.99) |
( | Use brackets to include negative numbers | (“%(f”, -99.99) | |
#If it is a floating point number, include the decimal point, if it is hexadecimal or octal, add 0x or 0 | (“%#x”, 99)(“%#o”, 99) | ||
##< | Format the previous one The parameters described by the conversion operator | ("%f and %<3.2f", 99.45) | |
d,% 2$s”, 99,”abc”) | 99,abc | ||
First one In the example, it is mentioned that %tx |
Includes full date and time information
F | "Year-Month-Day" format | 2007-10-27 |
---|---|---|
"Month/Day/Year" format | 10/27/07 | |
"HH:MM:SS PM" format ( 12-hour format) | 02:25:51 PM | |
"HH:MM:SS" format (24-hour format) | 14:28:16 | |
"HH:MM" format (24-hour format) | 14:28 | |
Let’s give an example to facilitate understanding | Date date=new Date(); //c的使用 System.out.printf("全部日期和时间信息:%tc%n",date); //f的使用 System.out.printf("年-月-日格式:%tF%n",date); //d的使用 System.out.printf("月/日/年格式:%tD%n",date); //r的使用 System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date); //t的使用 System.out.printf("HH:MM:SS格式(24时制):%tT%n",date); //R的使用 System.out.printf("HH:MM格式(24时制):%tR",date); Copy after login | Output results全部日期和时间信息:星期三 九月 21 22:43:36 CST 2016 年-月-日格式:2016-09-21月/日/年格式:16/10/21 HH:MM:SS PM格式(12时制):10:43:36 下午 HH:MM:SS格式(24时制):22:43:36 HH:MM格式(24时制):22:43 Copy after login |
java basic tutorial |
The above is the detailed content of What is the usage of String.format?. For more information, please follow other related articles on the PHP Chinese website!