Introduction to JAVA String.Format method usage

Source: Internet
Author: User
Tags iso 8601 iso 8601 format locale

Introduction to JAVA String.Format method usage

1. Format integers:%[index$][identification [minimum width] Conversion mode
We can see that the formatted string consists of 4 parts, where the meaning of%[index$] has been described above, and the meaning of [minimum width] is well understood, that is, the number of digits that ultimately the integer conversion string contains at least. Let's take a look at the meaning of the remaining 2 parts:

Logo:
'-' is left-aligned within the minimum width and cannot be used in conjunction with ' 0 padding '
' # ' only applies to 8 binary and 16 binary, 8 when adding a 0,16 in front of the result adds 0x to the front of the result
The ' + ' result always includes a symbol (normally only applies to 10 binary, if the object is BigInteger can be used for 8 binary and 16 binary)
"Positive value before adding a space, negative value before the minus sign (normally only applies to 10, if the object is BigInteger can be used for 8 binary and 16 binary)
' 0 ' result will be filled with zero
', ' applies only to 10 binary, with "," separating every 3 digits
' (' If the argument is negative, the result does not add a minus sign but enclose the number in parentheses (same as ' + ' ).' With the same restrictions)

Conversion Mode:
D-Decimal O-octal x or X-16 binary

The above instructions are too boring, let's look at a few specific examples. It is important to note that most of the identity 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 number: %[index$][logo [min. width] [precision] Conversion mode
As we can see, the conversion of floating-point numbers has a "precision" option that controls the number of digits after the decimal point.

Identity:
'-' is left-aligned within the minimum width and cannot be used in conjunction with ' 0 padding '
' + ' results always include a symbol
' Positive value plus a space before negative value plus minus sign
' 0 ' result will be filled with zero
', ' separated by ', ' between every 3 digits (for FGG conversions only)
' ('If the argument is negative, the result does not add a minus sign but enclose the number in parentheses (only for EEFGG conversions).

Conversion Mode:
' e ', ' e '--the result is formatted as a decimal number in computer science notation
' F '--the result is formatted as a decimal normal representation
' G ', ' G ' – depending on the case, the automatic selection of the normal representation or scientific notation method
' A ', ' a '--the result is formatted as a hexadecimal floating-point number with significant digits and exponent



3. Format the characters:
Formatting characters is very simple, C is a character, the identity '-' means left-aligned, and nothing else.

4. Format the percent symbol:
looking at the instructions above, you will find that the percent sign "%" is a prefix for a special format. So what do we do to enter a percent symbol? It is definitely necessary to escape the character, but note that the escape character here is not "\" but "%". In other words, the following statement can output a "12%":
System.out.println (String.Format ("%1$d%%", 12));

5. Get platform-independent line separators:
System.getproperty ("Line.separator") can get platform-independent line separators, but it is cumbersome to use the middle of format. The format function then comes with a platform-independent row delimiter that isString.Format ("%n").

6. Format the date type:
The following date and time conversion suffix characters are defined for the ' t ' and ' t ' conversions. These types are similar but not exactly equivalent to those defined by the GNU date and POSIX strftime (3c). Additional conversion types are provided to access Java-specific features, such as using ' L ' as milliseconds in seconds.

The following conversion characters are used to format the time:
' H ' 24-hour hour, which is formatted as necessary with a leading zero two-digit number, i.e. 00-23.
' I ' 12-hour hour, which is formatted as necessary with a leading-zero double-digit, i.e. 01-12.
The ' K ' Hour of 24 hours, that is, 0-23.
' L ' 12 hour hour, that's 1-12.
The minutes in ' M ' hours are formatted as necessary with a leading zero two-digit number, i.e. 00-59.
The seconds in ' s ' minutes, which are formatted as necessary with leading zeros in two digits, i.e. 00-60 ("60" is a special value required to support leap seconds).
The milliseconds in ' L ' seconds, formatted as necessary with a leading zero three-digit number, or 000-999.
The nanosecond in ' N ' seconds is formatted as necessary with a leading zero nine-digit number, or 000000000-999999999.
The ' P ' locale-specific morning or afternoon markers are indicated in lowercase, such as "AM" or "PM". Use the conversion prefix ' T ' to forcibly convert this output to uppercase.
The digital time zone offset of ' z ' in RFC 822 format relative to GMT, for example-0800.
' Z ' represents a string in the form of a time zone abbreviation. The locale of the Formatter will supersede the locale of the parameter, if any.
The number of seconds that have elapsed since Coordinated Universal Time (UTC) January 1, 1970 00:00:00 to now, that is, the difference between long.min_value/1000 and long.max_value/1000.
The number of milliseconds that ' Q ' has elapsed since Coordinated Universal Time (UTC) January 1, 1970 00:00:00 to now, that is, the difference between Long.min_value and Long.max_value.

The following conversion characters are used to format dates:
' B ' is locale-specific month full name, such as "January" and "February".
' B ' is a locale-specific month abbreviation, such as "Jan" and "Feb".
' H ' is the same as ' B '.
' A ' The full name of the day of the week that is specific to the locale, such as "Sunday" and "Monday"
' A ' week abbreviations specific to the locale, such as "Sun" and "Mon"
' C ' divided by 100 of the four digits of the year, is formatted as necessary with a leading zero of two digits, that is, 00-99
The ' Y ' year, formatted as necessary with a leading zero four-digit number (at least), for example, 0092 is equal to the Gregorian calendar of the sum of the CE.
The last two digits of the ' Y ' year, which are formatted as necessary with leading zeros of two digits, i.e. 00-99.
' J ' The number of days in a year, formatted as necessary with a leading zero of three digits, for example, for the Gregorian calendar is 001-366.
The ' m ' month, which is formatted as necessary with a leading zero two-digit number, i.e. 01-13.
The number of days in ' d ' one months, formatted as necessary with leading 2 digits, or 01-31
The number of days in ' e ' one months is formatted as two digits, or 1-31.

The following conversion characters are used to format common date/time combinations.
' R ' 24-hour time, formatted as "%TH:%TM"
' T ' 24-hour time is formatted as "%th:%tm:%ts".
The ' R ' 12-hour time is formatted as "%ti:%tm:%ts%TP". The location of the morning or afternoon mark ('%TP ') may be related to the locale.
The ' D ' date is formatted as "%tm/%td/%ty".
The full date of the ' F ' ISO 8601 format is formatted as "%TY-%TM-%TD".
The ' C ' date and time are formatted as "%ta%TB%td%tt%tz%ty", such as "Sun Jul 16:17:00 EDT 1969".

Introduction to JAVA String.Format method usage

Related Article

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.