The String.Format in Java uses

Source: Internet
Author: User
Tags locale string format time and date

The format (String format, objece ... argues) function is equivalent to the printf function in the C language, but is relatively flexible. Similar to the printf function in C, a simple substitution effect can be generated when format is used. Replace the corresponding conversion character with a specific value. The translator has the following: Passing in the first parameter of format can represent a specific message. In the second parameter to pass in the specific value to complete the corresponding substitution, the equivalent can have a certain template mode, and then in the actual fill with a specific value to fill.

String Str=null;

Str=string.format ("hi,%s", "dragon"); formatting strings

System.out.println (str); Output string Variable str content

System.out.printf ("Uppercase of the letter A is:%c%n", ' a ');

System.out.printf ("3>7 results are:%b%n", 3>7);

System.out.printf ("Half of 100 is:%d%n", 100/2);

System.out.printf ("100 of 16 binary numbers are:%x%n", 100);

System.out.printf ("100 of 8 binary numbers are:%o%n", 100);

System.out.printf ("50 yuan book 8.5 discount is:%f Yuan%n", 50*0.85);

System.out.printf ("16 of the above price is:%a%n", 50*0.85);

System.out.printf ("The index of the above price indicates:%e%n", 50*0.85);

System.out.printf ("The index of the above price and the length of the floating point result is shorter:%g%n", 50*0.85);

System.out.printf ("The above discount is%d%%%n", 85);

System.out.printf ("The hash code for the letter A is:%h%n", ' a ');

The format parameter can contain strings that do not need to be converted, and these strings are what you write and ultimately what you output. It also contains special-format content that specifies which object to convert, and what form to convert. This special format begins with%index$, and index starts at 1, indicating that the index parameter is taken in to format. This is a bit stronger than the C language, C language can only be formatted in order of the parameters, and Java can choose the nth parameter to format.  Because the function can format any object, different objects will have different parameters, so you should distinguish between them.     1. Format the integer:%[index$][identity [min. width] Conversion Format The string is composed of four parts.            %[index$]---To format index parameters [identity]---the appropriate format of the configuration information, such as when less than the minimum width of how to handle, the identity includes: '-' in the minimum width of the left-aligned (by default, right-aligned), can not be used with the "0 fill" ' # ' 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 is enclosed in parentheses (with the same restriction as ' + ') [the conversion mode] actually represents what the binary representation of the incoming number is. The conversion method consists of the following categories: D-Decimal O-octal x or X-16 binary date and time string formatting

In the program interface often need to display the time and date, but the format of the display is often unsatisfactory, you need to write a lot of code through various algorithms to get the ideal date and time format. There is also a%tx converter in the string format that is not described in detail and is specifically used to format dates and times. The x in the%TX Converter represents an additional conversion that handles date and time formats , and their combination can format dates and times into multiple formats.

1. Common datetime formatting A date-time-formatted converter defines various ways to format date strings, with the most common combinations of dates and times formatted as follows: Use "%T[C/F/D/R/T/R" when used Such a form can be.----------------------------------------------------------------------------------------------------------------- ----------

Date Date=new date (); Create Date Object

System.out.printf ("All date and time information:%tc%n", date); Format output date or time

SYSTEM.OUT.PRINTF ("year-month-day format:%tf%n", date);

SYSTEM.OUT.PRINTF ("Month/day/year format:%td%n", date);

System.out.printf ("HH:MM:SS PM format (12 o'clock):%tr%n", date);

System.out.printf ("HH:MM:SS format (24 o'clock):%tt%n", date);

System.out.printf ("hh:mm format (24 o'clock):%tr", date);

--------------------------------------------------------------------------------------------------------------- ------------in the processing of the date will also encounter is to obtain the current month, is the first day of this year such problems, such a problem can also be obtained in String.Format this function. Date date = new Date (); System.out.println ("%TB", date)//This is the month to get the operating system language, Chinese is of course the Chinese System.out.println (locale.us, "%TB", date)// This is the abbreviation for the month of getting the English format time string

The conversion of the time format is more and more accurate than the date format conversion character. It can format time into units of hours, minutes, seconds, or even milliseconds. The conversion of the format time string is shown in the following table:

--------------------------------------------------------------------------------------------------------------- ----------------

Date Date=new date (); Create Date Object

System.out.printf ("2-digit 24 o'clock hour (less than 2 bits in front 0):%th%n", date);

System.out.printf ("2-digit 12 o'clock hour (less than 2 bits in front 0):%ti%n", date);

System.out.printf ("2-digit 24 o'clock hour (front not 0):%tk%n", date);

System.out.printf ("2-digit 12 o'clock hour (front not 0):%tl%n", date);

System.out.printf ("2 digits in minutes (less than 2 bits before 0):%tm%n", date);

System.out.printf ("2 digit seconds (less than 2 bits in front of 0):%ts%n", date);

System.out.printf ("3 digits in milliseconds (less than 3 bits before 0):%tl%n", date);

System.out.printf ("9 digits in milliseconds (less than 9 bits in front of 0):%tn%n", date);

String Str=string.format (locale.us, "small letter morning or afternoon mark (English):%TP", date);

System.out.println (str); Output string Variable str content

System.out.printf ("lowercase letter of the morning or afternoon mark (medium):%tp%n", date);

System.out.printf ("Offset from RFC822 time zone relative to GMT:%tz%n", date);

System.out.printf ("time zone abbreviation string:%tz%n", date);

System.out.printf (number of seconds from 1970-1-1 00:00:00 to now:%ts%n, date);

SYSTEM.OUT.PRINTF (the number of milliseconds elapsed from 1970-1-1 00:00:00 to now:%tq%n, date);

--------------------------------------------------------------------------------------------------------------- ----------------reference from: http://zhangwenzhuo.iteye.com/blog/193754 http://kgd1120.iteye.com/blog/1293633

The String.Format in Java uses

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.