Format (Locale l, String format, Object... args) returns a formatted String using the specified language environment, format String, and parameters.
Format (String format, Object... args) returns a formatted String using the specified format String and parameters.
CODE:
Long now = System. currentTimeMillis ();
CODE:
// Current month/day/year
CODE:
S = String. format ("%, d", Integer. MAX_VALUE); // "2,147,483,647"
CODE:
S = String. format ("% 05d", 123); // "00123" is it very convenient and exciting? Haha, there are more results!
String s = String. format ("Duke's Birthday: % 1 $ tm % 1 $ te, % 1 $ tY", c );
:
The optional flags are character sets that modify the output format. The set of valid flag depends on the Conversion Type.
The optional precision is a non-negative decimal integer, which is usually used to limit the number of characters. The specific action depends on the Conversion Type.
The required conversion is a character that indicates how to format parameters. The valid conversion set of a given parameter depends on the Data Type of the parameter.
2. the syntax used to indicate format specifiers of Date and Time types is as follows:
% [Argument_index $] [flags] [width] conversion
The required conversion is a sequence of two characters. The first character is 'T' or 'T '. The second character indicates the format used. These characters are similar to but not completely equivalent to those defined by GNU date and POSIX strftime (3c.
:
% [Flags] [width] conversion
The required conversion is a character that indicates the content to be inserted in the output.
Conversion can be divided into the following types:
1. General-applicable to any parameter type
2. Character-it can be applied to the basic types of Unicode characters: char, Character, byte, Byte, short, and Short. When Character. isValidCodePoint (int) returns true, you can apply this conversion to int and Integer types.
3. Value
1. Integer-Integer types that can be applied to Java: byte, Byte, short, Short, int, Integer, long, Long, And BigInteger
2. Floating Point-applicable to Java floating point types: float, Float, double, Double, and BigDecimal
4. Date/time-types that can be applied to Java and can be encoded for Date or time: long, Long, Calendar, and Date.
5. Percentage-generate the nominal value '%' ('\ u0025 ')
6. Line separator-generate a platform-specific line Separator
Java code
- Double d = 12.3366;
- System. out. println (String. format ("% 2 $08d",-3123,-5566 ));
- System. out. println (String. format ("% 1 $9d",-31 ));
- System. out. println (String. format ("% 1 $-9d",-31 ));
- System. out. println (String. format ("% 1 $(9d",-31 ));
- System. out. println (String. format ("% 1 $ # 9x", 5689 ));
- // Two digits after the decimal point
- System. out. println (String. format ("% 1 $. 2f", 5689.0); // must be of the same type before conversion
- // Format the location
- /* String str = "I love ni % s, you love me % s ";
- String str2 = "I love ni % 2 $ s, you love me % 1 $ s ";
- System. out. println (String. format (str, "renjunjie", "songliyu "));
- System. out. println (String. format (str2, "renjunjie", "songliyu "));*/
- // Array Operations
- Object [] sendData = new Object [4];
- SendData [0] = Integer. valueOf (1 );
- SendData [1] = "172.12.1.2 ";
- SendData [2] = Integer. valueOf (123 );
- SendData [3] = "testadfaerfa ";
- String sendDataString = String. format ("% d, % s, % d, % s", (Object []) sendData );
- System. out. println (sendDataString );