This article introduces the use of JAVA string formatting-String.format(), as follows:
Format of regular types
The format() method of the String class is used to create formatted strings and concatenate multiple string objects. Students who are familiar with C language should remember the sprintf() method of C language, and there are similarities between the two. The format() method has two overloaded forms.
format(String format, Object... args) New string uses the local locale to create string formats and parameters to generate formatted new strings.
format(Locale locale, String format, Object... args) Use the specified locale to create string formats and parameters to generate formatted strings.
Show different converters to implement conversion of different data types to strings, as shown in the figure.
Converter | illustrate | Example |
%s | String type | "mingrisoft" |
%c | Character Type | 'm' |
%b | Boolean type | true |
%d | Integer type (decimal) | 99 |
%x | Integer type (hexadecimal) | FF |
%o | Integer type (octal) | 77 |
%f | Floating point type | 99.99 |
%a | Hexadecimal floating point type | FF.35AE |
%e | Index Type | 9.38e+5 |
%g | General floating point types (shorter of f and e types) | |
%h | Hash code | |
%% | Percent Type | % |
%n | Line breaks | |
%tx | Date and time type (x represents different date and time conversion characters |
Test cases
public static void main(String[] args) { String str=null; str=Stringformat("Hi,%s", "Wang Li"); Systemoutprintln(str); str=Stringformat("Hi,%s:%s%s", "Wang Nan", "Wang Li", "Wang Zhang"); Systemoutprintln(str); Systemoutprintf("The uppercase letter a is: %c %n", 'A'); Systemoutprintf("3>7 result is: %b %n", 3>7); Systemoutprintf("Half of "100 is: %d %n", 100/2); Systemoutprintf("100's hexadecimal number is: %x %n", 100); Systemoutprintf("100's angular number is: %o %n", 100); Systemoutprintf("50 yuan book is 5 discount: %f %n", 50*85); Systemoutprintf("The hexadecimal number of the above price is: %a %n", 50*85); Systemoutprintf("The index of the above price represents: %e %n", 50*85); Systemoutprintf("The shorter length of the index of the above price and the floating point number result is: %g %n", 50*85); Systemoutprintf("The discount above is %d%%% %n", 85); Systemoutprintf("The hash code of letter A is: %h %n", 'A'); }Output result
Hi, Wang Li Hi, Wang Nan: Wang Li Wang Zhang's letter a is capitalized: A 3>7 The result is: false Half of 100 is: 50 100 is: 64 100 is 80 144 50 yuan book 5 discount is: 500000 yuan The hexadecimal number above is: 0x54p5 The index above is expressed: 250000e+01 The length of the index above and the floating point number above is shorter: 50000 The discount above is 85% The hash code of letter A is: 41
The logo paired with the conversion symbol is shown in the figure.
| Logo | illustrate | Example | result |
| + | Add symbols for positive or negative numbers | ("%+d",15) | +15 |
| - | Left aligned | ("%-5d",15) | |15| |
| 0 | Add 0 before the number | ("%04d", 99) | 0099 |
| Spaces | Add a specified number of spaces before an integer | ("% 4d", 99) | | 99| |
| , | Group numbers with "," | ("%,f", 9999.99) | 9,999.990000 |
| ( | Use brackets to include negative numbers | ("%(f", -99.99) | (99.990000) |
| # | If it is a floating point, it contains a decimal point. If it is hexadecimal or ength, it adds 0x or 0 | ("%#x", 99) | ("%#o", 99) |
| < | Format the parameters described by the previous converter | ("%f and %<3.2f", 99.45) | 99.450000 and 99.45 |
| $ | Formatted parameter index | ("%1$d,%2$s", 99,"abc") | 99,abc |
Test cases
public static void main(String[] args) { String str=null; //$Use str=Stringformat("Use format parameter $: %1$d,%2$s", 99,"abc"); Systemoutprintln(str); //+Use Systemoutprintf("Symbols showing positive and negative numbers: %+d and %d%n", 99,-99); //Complete OUse Systemoutprintf("The most awesome number is: %03d%n", 7); //Use Systemoutprintf("The effect of Tab key is: %8d%n", 7); //Use Systemoutprintf("The effect of integer grouping is: %,d%n", 9989997); //Space and decimal point number Systememoutprintf("The price of a book is: % 5f yuan %n", 8); }Output result
The use of format parameter $: 99,abc Symbols that display positive and negative numbers: +99 and -99 The most awesome number is: 007 The effect of the Tab key is: 7 The effect of integer grouping is: 9,989,997 The price of a book is: 80,000 yuan
Date and event string formatting
Time and date are often needed to display in the program interface, but the format of the display is often unsatisfactory. A large amount of code is required to go through various algorithms to obtain the ideal date and time format. There is also a %tx converter in the string format that is not detailed, it is specially used to format dates and times. x in the %tx converter represents an additional converter that processes date and time formats, and their combinations can format date and time into multiple formats.
Common formats for date and time combinations, as shown in the figure.
| Converter | illustrate | Example |
| c | Includes all date and time information | Saturday October 27 14:21:20 CST 2007 |
| F | "Year-Month-Day" format | 2007-10-27 |
| D | "Month/Day/Year" format | 10/27/07 |
| r | "HH:MM:SS PM" format (12-hour system) | 02:25:51 pm |
| T | "HH:MM:SS" format (24-hour system) | 14:28:16 |
| R | "HH:MM" format (24-hour system) | 14:28 |
Test cases
public static void main(String[] args) { Date date=new Date(); //c uses Systemutprintf("All date and time information: %tc%n",date); //f uses Systemutprintf("Year-month-day-day format: %tF%n",date); //d uses Systemutprintf("Month/Day/Year format: %tD%n",date); //r uses Systemutprintf("HH:MM:SS PM format (12 time system): %tt%n",date); //t uses Systemutprintf("HH:MM:SS format (24 time system): %tT%n",date); //R uses Systemutprintf("HH:MM format (24 time format): %tR", date); }Output result
All date and time information: Monday September 10 10:43:36 CST 2012-month-day format: 2012-09-10 Month/Day/Year format: 09/10/12 HH:MM:SS PM format (12 o'clock): 10:43:36 Morning HH:MM:SS format (24 o'clock): 10:43:36 HH:MM format (24 o'clock): 10:43
A converter that defines the date format can cause the date to generate a new string through the specified converter. These date converters are shown in the figure.
public static void main(String[] args) { Date date=new Date(); //b's use, month abbreviation is String str=Stringformat(LocaleUS,"English month abbreviation: %tb",date); Systemoutprintln(str); Systemoutprintf("local month abbreviation: %tb%n",date); //B's use, month full name str=Stringformat(LocaleUS,"English month full name: %tB",date); Systemoutprintln(str); Systemoutprintf("local month full name: %tB%n",date); //The use of a, week is abbreviated as str=Stringformat(LocaleUS,"English week's abbreviation: %ta",date); Systemoutprintln(str); //The use of A, week's full name Systemoutprintf("local week's abbreviation: %tA%n",date); //The use of C, two digits Systemoutprintf("The first two digits of the year (less than two digits before the first): %tC%n",date); //The use of y, two digits Systemoutprintf("The last two digits of the year (less than two digits before the first): %ty%n",date); //J, the number of days in a year Systemutprintf("Days in a year (that is, the day of the year): %tj%n",date); //M, the month Systemutprintf("Month of two digits (less than two digits before 0): %tm%n",date); //D, the day (two digits, not enough to make up for zeros) Systemutprintf("Day of two digits (less than two digits before 0): %td%n",date); //E, the day (one digits before 0) Systemutprintf("Day of two digits (less than two digits before 0): %td%n",date); //E, the day (one digits before 0) Systemutprintf("Day of month (not enough to make up for zeros): %te",date); }Output result
English month abbreviation: Sep Local month abbreviation: September English month full name: September full name: September English week abbreviation: Mon Local week abbreviation: The first two digits of the week (before less than two digits, add 0): 20 The last two digits of the year (before less than two digits, add 0): 12 The number of days in the year (that is, what day of the year): 254 The month of the two digits (before less than two digits, add 0): 09 The day of the two digits (before less than two digits, add 0): October day (before less than two digits, add 0): 10
Compared with date format converters, time format converters are more and more accurate. It can format time into units such as hours, minutes, seconds, and even hour milliseconds. The converter for formatting the time string is shown in the figure.
| Converter | illustrate | Example |
| H | 2-digit number 24 hours (below 2 digits, 0 is added before the first) | 15 |
| I | 2-digit number 12 hours (less than 2 digits before adding 0) | 03 |
| k | 2-digit number 24 hour time (0 is not added before) | 15 |
| l | 2-digit number 12 hours (0 is not added before) | 3 |
| M | Minutes of 2 digits (less than 2 digits before adding 0) | 03 |
| S | Seconds of 2 digits (below 2 digits, add 0 in front) | 09 |
| L | Milliseconds of 3 digits (less than 3 digits before adding 0) | 015 |
| N | The number of milliseconds of 9 digits (below 9 digits, add 0 before) | 562000000 |
| p | Morning or afternoon markings for lowercase letters | Medium: afternoon English: pm |
| z | Offset relative to GMT's RFC822 time zone | +0800 |
| Z | Time zone abbreviation string | CST |
| s | 1970-1-1 00:00:00 The number of seconds passed to now | 1193468128 |
| Q | 1970-1-1 00:00:00 The number of milliseconds passed to now | 1193468128984 |
Test code
public static void main(String[] args) { Date date = new Date(); //H uses Systemutprintf("2-digit 24 hour time system (before 0 if less than 2 digits): %tH%n", date); //I uses Systemutprintf("2-digit 12 hour time system (before 0 if less than 2 digits): %tI%n", date); //k uses Systemutprintf("2-digit 24 hour time system (before 0 if not 0 if not 0 if not 0 if not 0 if not 0 if not l"); //l uses Systemutprintf("2-digit 12 hour time system (before 0 if not 0 if not 0 if not 0 if not 0 if not 0 if not l); //M uses Systemutprintf("Minutes of 2 digits (before 0 if less than 2 digits): %tM%n", date); //S uses Systemutprintf("Seconds of 2 digits (before 0 if less than 2 digits): %tS%n", date); //L uses Systemutprintf("Minutes of 3 digits (before 0 if less than 3 digits): %tL%n", date); //N uses Systemutprintf("Minutes of 9 digits (before 0 if less than 9 digits): %tN%n", date); //P uses String str = Stringformat(LocaleUS, "Morning or afternoon marks of lowercase letters (English): %tp", date); Systemoutprintln(str); Systemoutprintf("Morning or afternoon marks of lowercase letters (medium): %tp%n", date); //z's use Systemoutprintf("Offset relative to GMT's RFC822 time zone: %tz%n", date); //Z's use Systemoutprintf("Time zone abbreviation string: %tZ%n", date); //s's use Systemoutprintf("1970-1-1 00:00:00 The number of seconds passed to the present: %ts%n", date); //Q's use Systemoutprintf("1970-1-1 00:00:00 The number of milliseconds that have passed so far: %tQ%n", date); }Output result
2-digit 24 hour hours (before 0 if less than 2 digits): 11 2-digit 12 hour hours (before 0 if less than 2 digits): 11 2-digit 24 hour hours (before 0 if less than 2 digits): 11 2-digit 12 hour hours (before 0 if less than 2 digits): 11 2-digit 12 hour hours (before 0 if less than 2 digits): 11 2-digit digit minutes (before 0 if less than 2 digits): 03 2-digit digit seconds (before 0 if less than 2 digits): 52 3-digit digit milliseconds (before 0 if less than 3 digits): 773 9-digit digit milliseconds (before 0 if less than 9 digits): 7730000000 Morning or afternoon marks for lowercase letters (English): am Morning or afternoon marks for lowercase letters (Medium): offset of morning relative to GMT's RFC822 time zone: +0800 Time zone abbreviation string: CST 1970-1-1 00:00:00 The number of seconds passed to the present: 1347246232 1970-1-1 00:00:00 The number of milliseconds passed to the present: 1347246232773
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.