Usage of string. format in Java

Source: Internet
Author: User
Tags iso 8601 iso 8601 format
Java string. Format

1. format the integer: % [index $] [ID] [minimum width] Conversion Method
We can see that the formatted string consists of four parts. The meaning of % [index $] is described above, and the meaning of [minimum width] is also well understood, the minimum number of digits in the string to be converted. Let's take a look at the meanings of the remaining two parts:

Identifier:
'-' Is left aligned within the minimum width. It cannot be used together with "fill with 0 ".
'#' Is only applicable to octal and hexadecimal. In octal, a hexadecimal value is added before the result and 0x is added before the result.
The '+' result always contains a symbol (generally, it is only applicable to hexadecimal notation. if the object is biginteger, it can be used in hexadecimal notation and hexadecimal notation)
''Plus space before a positive value and negative value plus a negative number (generally only applicable to the 10-digit system. If the object is biginteger, it can be used in the 8-and 16-digit systems)
The '0' result is filled with zero.
',' Is only applicable to the 10-digit system. Each three digits are separated by commas (,).
'(If the parameter is a negative number, enclose The number in parentheses instead of adding a negative number in the result (the same as' +)

Conversion Method:
D-decimal o-octal X or X-hexadecimal

The above description is too boring. Let's look at some specific examples. Note that most of the logo characters can be used at the same time.

System. Out. println (string. Format ("% 1 $, 09d",-3123 ));
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 ));

// The result is:
//-0003,123
//-31
//-31
// (31)
// 0x1639


2. format the floating point: % [index $] [ID] [minimum width] [. Precision] Conversion Method
We can see that the floating point conversion has an "precision" option, which can control the number of digits after the decimal point.

Identifier:
'-' Is left aligned within the minimum width. It cannot be used together with "fill with 0 ".
The '+' result always contains a symbol.
''Plus spaces before positive values and minus signs before negative values
The '0' result is filled with zero.
',' Each three digits are separated by commas (,) (only applicable to fgg conversions)
'(If the parameter is a negative number, enclose The number in parentheses instead of adding a negative number in the result (only applicable to the conversion of eefgg)

Conversion Method:
'E', 'E' -- the result is formatted as a decimal number in the computer science notation.
'F' -- the result is formatted as a normal decimal representation.
'G', 'G' -- automatically selects the normal expression or scientific notation method based on the actual situation.
'A', 'A' -- the result is formatted as a hexadecimal floating point with valid digits and indexes.


3. format the characters:
It is very easy to format characters. C indicates the characters, and '-' indicates the left alignment in the logo. Nothing else.

4. format the percent sign:
After reading the preceding instructions, you will find that the percent sign "%" is a prefix in a special format. What should we do if we enter a percent sign? It must be escaped, but note that the escape character here is not "\", but "% ". In other words, the following statement can output "12% ":
System. Out. println (string. Format ("% 1 $ d %", 12 ));

5. Obtain the Independent Line Separator of the Platform:
System. getproperty ("line. separator") can be used to obtain independent row delimiters on the platform, but it is too cumbersome to use them in the format. Therefore, the format function comes with a platform-independent line separator, Which is string. Format ("% N ").

6. format the date type:
The following Date and Time conversion suffixes are defined for 'T' and 'T' conversion. These types are similar to but not exactly equivalent to those defined by GNU date and POSIX strftime (3C. Other conversion types are provided to access Java-specific functions (for example, using 'l' as the millisecond in seconds ).

The following Conversion characters are used to format the time:
The hour in the 'H' 24-hour format is formatted as two digits with leading zeros (00-23) if necessary.
The hour in the 'I' 12-hour format is formatted as the two digits with the leading zero when necessary, that is, 01-12.
The hour in the 'K' 24-hour format, that is, 0-23.
Hours in the 'l' 12-hour format, that is, 1-12.
Minutes in the 'M' hour are formatted as two digits with leading zeros (00-59) if necessary.
Seconds in 's' minutes are formatted as two digits with leading zeros when necessary, that is, 00-60 ("60" is a special value required to support leap seconds ).
The millisecond in 'l' seconds is formatted as a three-digit number with leading zero when necessary, that is, 000-999.
The millisecond In 'n' seconds is formatted as the nine-digit number with the leading zero when necessary, that is, 000000000-999999999.
'P' indicates the language environment in lowercase in the morning or afternoon, for example, "am" or "PM ". You can use the conversion prefix 'T' to forcibly convert the output to an upper-case format.
The offset of 'Z' to the digital time zone in RFC 822 format of GMT, for example,-0800.
'Z' indicates a string in the abbreviated form of time zone. The formatter language environment replaces the parameter language environment (if any ).
The number of seconds since the Coordinated Universal Time (UTC) on January 1, 1970 00:00:00 to the present, that is, the difference between long. min_value/1000 and long. max_value/1000.
'Q' indicates the number of milliseconds that have elapsed since 00:00:00 on January 1, January 1, 1970 in Coordinated Universal Time (UTC), that is, the difference between long. min_value and long. max_value.

The following Conversion characters are used to format the date:
'B' is the full name of the month specific to the language environment, such as "January" and "February ".
'B' indicates the abbreviation of the month specific to the language environment, such as "Jan" and "FEB ".
'H' is the same as 'B.
'A is the full name of the day of the week specific to the language environment, such as "Sunday" and "Monday"
'A refers to the day of the week (such as "sun" and "mon") specific to the language environment"
'C' is divided by four digits of 100 to indicate the year. It is formatted as two digits with leading zeros (00-99) if necessary.
'Y' year, formatted as a four-digit (at least) with a leading zero when necessary, for example, 0092 equals 92 CE of the Gregorian calendar.
The last two digits of the 'y' year are formatted as the two digits with the leading zero when necessary, that is, 00-99.
The number of days in a year of 'J' is formatted as a three-digit number with a leading zero when necessary. For example, for a Gregorian calendar, the value is 001-366.
The 'M' month is formatted as a two-digit with a leading zero when necessary, that is, 01-13.
'D' the number of days in a month, which is formatted as the first zero or two digits (01-31) if necessary.
The number of days in a month is formatted as two digits, that is, 1-31.

The following Conversion characters are used to format common date/time combinations.
'R' 24-hour time, formatted as "% TH: % TM"
The time in the 'T' 24-hour format is formatted as "% TH: % TM: % ts ".
'R' 12-hour time, formatted as "% Ti: % TM: % TS % TP ". The location marked in the morning or afternoon ('% TP') may be related to the language environment.
'D' date, formatted as "% TM/% TD/% ty ".
'F' complete date in ISO 8601 format, formatted as "% ty-% TM-% TD ".
'C' date and time, formatted as "% TA % TB % TD % TT % TZ % ty", for example, "Sun Jul 20 16:17:00 EDT 1969 ".

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.