Java String. Format () method and parameter description

Source: Internet
Author: User

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
    1. Double d = 12.3366;
    2. System. out. println (String. format ("% 2 $08d",-3123,-5566 ));
    3. System. out. println (String. format ("% 1 $9d",-31 ));
    4. System. out. println (String. format ("% 1 $-9d",-31 ));
    5. System. out. println (String. format ("% 1 $(9d",-31 ));
    6. System. out. println (String. format ("% 1 $ # 9x", 5689 ));
    7. // Two digits after the decimal point
    8. System. out. println (String. format ("% 1 $. 2f", 5689.0); // must be of the same type before conversion
    9. // Format the location
    10. /* String str = "I love ni % s, you love me % s ";
    11. String str2 = "I love ni % 2 $ s, you love me % 1 $ s ";
    12. System. out. println (String. format (str, "renjunjie", "songliyu "));
    13. System. out. println (String. format (str2, "renjunjie", "songliyu "));*/
    14. // Array Operations
    15. Object [] sendData = new Object [4];
    16. SendData [0] = Integer. valueOf (1 );
    17. SendData [1] = "172.12.1.2 ";
    18. SendData [2] = Integer. valueOf (123 );
    19. SendData [3] = "testadfaerfa ";
    20. String sendDataString = String. format ("% d, % s, % d, % s", (Object []) sendData );
    21. System. out. println (sendDataString );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.