Common Java String formatting String. format usage, javastring. format

Source: Internet
Author: User

Common Java String formatting String. format usage, javastring. format
Regular formatting

The format () method of the String class is used to create formatted strings and connect multiple String objects. If you are familiar with C, you should remember the sprintf () method of C. The two have similarities. The format () method has two reloads.

Format (String format, Object... args) The new String uses the local language environment to create the String format and parameters to generate the formatted new String.

Format (Locale locale, String format, Object... args) uses the specified language environment to create String formats and parameters to generate formatted strings.

Display different Conversion characters to convert different data types to strings ,.

Conversion character

Description

Example

% S

String type

"Mingrisoft"

% C

Character Type

'M'

% B

Boolean Type

True

% D

INTEGER (decimal)

99

% X

Integer type (hexadecimal)

FF

% O

Integer type (octal)

77

% F

Floating Point Type

99.99

%

Hexadecimal floating point type

FF.35AE

% E

Exponential type

9.38e + 5

% G

General floating point type (shorter of the f and e types)

 

% H

Hash code

 

%

Percentage type

%

% N

Line Break

 

% Tx

Date and Time type (x represents different date and time Conversion characters

 

Test Cases

Public static void main (String [] args) {String str = null; str = String. format ("Hi, % s", "Wang Li"); System. out. println (str); str = String. format ("Hi, % s: % s. % s "," Wang Nan "," Wang Li "," Wang Zhang "); System. out. println (str); System. out. printf ("uppercase letter a: % c % n", 'A'); System. out. printf ("3> 7: % B % n", 3> 7); System. out. printf ("half of 100 is: % d % n", 100/2); System. out. printf ("The hexadecimal number of 100 is: % x % n", 100); System. out. printf ("the octal number of 100 is: % o % n", 100); System. out. printf ("50 yuan book 8.5 discount: % f yuan % n", 50*0.85); System. out. printf ("The hexadecimal number of the above price is: % a % n", 50*0.85); System. out. printf ("the above price index: % e % n", 50*0.85); System. out. printf ("the result length of the above price index and floating point number is shorter: % g % n", 50*0.85); System. out. printf ("the discount above is % d % n", 85); System. out. printf ("the hash code for letter A is: % h % n", 'A ');}

Output result

Hi, Wang Li Hi, Wang Nan: Wang Li. the uppercase letter a is: A 3> 7. The result is: false. Half of 100 is: 50. 100 hexadecimal number is: 64. 100. 8 hexadecimal number is: 144 discount for 50 RMB books: 8.5 RMB The hexadecimal number of the above price is: 0 X1. 54p5 above the price index: 4.2520.e + 01 above the price index and floating point number results in A short length is: 42.5000 above the discount is 85% letter A hash code is: 41

Match the symbol of the conversion character ,.

Logo

Description

Example

Result

+

Add a sign to a positive or negative number

("% + D", 15)

+ 15

−

Left aligned

("%-5d", 15)

| 15 |

0

Add 0 before the number

("% 04d", 99)

0099

Space

Add a specified number of spaces before the integer

("% 4d", 99)

| 99 |

,

Group numbers ","

("%, F", 9999.99)

9,999.990000

(

Use parentheses to include negative numbers

("% (F",-1, 99.99)

(99.990000)

#

If it is a floating point, it contains the decimal point. If it is a hexadecimal or octal, add 0x or 0.

("% # X", 99)

("% # O", 99)

0x63

0143

<

Format the parameters described by the previous conversion Operator

("% F and % <3.2f", 99.45)

99.450000 and 99.45

$

Formatted parameter index

("% 1 $ d, % 2 $ s", 99, "abc ")

99, abc

Test Cases

Public static void main (String [] args) {String str = null; // $ use str = String. format ("use of format parameter $: % 1 $ d, % 2 $ s", 99, "abc"); System. out. println (str); // + use System. out. printf ("symbols for displaying Positive and Negative Numbers: % + d and % d % n", 99,-99); // Use System for completing O. out. printf ("the best number is % 03d % n", 7); // Use System with spaces. out. printf ("the effect of the Tab key is % 8d % n", 7 );//. use System. out. printf ("integer grouping effect: %, d % n", 9989997); // space and number of digits after the decimal point System. out. printf ("the price of a book is: % 50.5f yuan % n", 49.8 );}

Output result

The format parameter $ is used: 99. The positive and negative characters of abc are displayed: + 99 and-99. The maximum number is: 007. The effect of the Tab key is: 7 integer grouping: 9,989,997 the price of a book is: 49.80000 yuan
Date and event string formatting

The display time and date are often required on the program interface, but the display format is often unsatisfactory. You need to write a lot of code to get the ideal date and time format through various algorithms. The % tx conversion character is not detailed in the string format. It is specially used to format the date and time. X in the % tx conversion operator represents another conversion operator that processes the date and time formats. Their combination can format the date and time into multiple formats.

Common Date and Time combination formats ,.

Conversion character

Description

Example

C

Including all date and time information

Week 60 month 27 14:21:20 CST 2007

F

"Year-month-day" Format

2007-10-27

D

"Month/day/year" Format

10/27/07

R

"HH: MM: ss pm" format (in 12-hour format)

02:25:51 pm

T

"HH: MM: SS" format (in 24-hour format)

14:28:16

R

"HH: MM" format (in 24-hour format)

14: 28

Test Cases

Public static void main (String [] args) {Date date = new Date (); // c uses System. out. printf ("all date and time information: % tc % n", date); // f System. out. printf ("year-month-day format: % tF % n", date); // Use System for d. out. printf ("month/day/year format: % tD % n", date); // r use System. out. printf ("HH: MM: ss pm format (12-hour format): % tr % n", date); // Use System for t. out. printf ("HH: MM: SS format (in 24-hour format): % tT % n", date); // R using System. out. printf ("HH: MM format (in 24-hour format): % tR", date );}

Output result

All date and time information: Monday September 10 10:43:36 CST 2012-month-day format: Month/day/year format: 09/10/12 HH: MM: ss pm format (12 hour): 10: 43: 36 am HH: MM: SS format (in 24-hour format): 10: 43: 36 HH: MM format (in 24-hour format): 10: 43

The conversion operator that defines the date format can generate a new string using the specified conversion token. These date delimiters.

Public static void main (String [] args) {Date date = new Date (); // use of B, for short, String str = String. format (Locale. US, "abbreviated month: % tb", date); System. out. println (str); System. out. printf ("Local month Abbreviation: % tb % n", date); // use of B, full name of month str = String. format (Locale. US, "Full name of English month: % tB", date); System. out. println (str); System. out. printf ("Local month full name: % tB % n", date); // Use of a, str = String for weeks. format (Locale. US, "% ta", date); System. out. println (str); // Use of A, the full name of the week is System. out. printf ("Local week Abbreviation: % tA % n", date); // C usage, two systems a year ago. out. printf ("the first two digits of the Year (less than two digits of the first digit fill 0): % tC % n", date); // use of y, two systems after the year. out. printf ("the last two digits of the Year (less than two digits are preceded by 0): % ty % n", date); // j, the number of days of the year System. out. printf ("days in a year (that is, the day of the year): % tj % n", date); // use of m, month System. out. printf ("two-digit month (less than two first 0): % tm % n", date); // use of d, Day (two, Less Than Zero) System. out. printf ("two-digit Day (less than two first 0): % td % n", date); // use of e, day (one digit is not zero) System. out. printf ("day of the month (0 is not added before): % te", date );}

Output result

Abbreviation of the English month: Sep local month Abbreviation: September English month full name: September local month full name: September English week Abbreviation: Mon local week Abbreviation: the first two digits of the year of Monday (less than two digits plus 0): the last two digits of the year of 20 (less than two digits plus 0): The day of the year of 12 (that is, the day of the year ): 254 double-digit month (0 for less than two digits): 09 double-digit Day (0 for less than two digits): day for January (0 for less than two digits): 10

Compared with the date format conversion operator, the time format conversion operator is more accurate. It can format the time to a unit of time, minute, second, or even hour millisecond. Format the time String Conversion character.

 

Conversion character

Description

Example

H

The hour in the 24-digit format (0 for less than two digits)

15

I

The hour in 12-digit notation (0 for less than two digits)

03

K

The hour in the 24-digit format (the first digit is not 0)

15

L

The hour in 12-digit notation (0 is not added before)

3

M

Minutes of two digits (0 before less than two digits)

03

S

The second of two digits (0 before less than two digits)

09

L

The millisecond value of a three-digit number (0 before 3 digits)

015

N

9-digit millisecond count (0 before 9 digits)

562000000

P

Morning or afternoon Mark of lowercase letters

Moderate: afternoon

English: pm

Z

Offset of the RFC822 Time Zone relative to GMT

+ 0800

Z

Time zone abbreviation string

CST

S

The number of seconds that have elapsed since 00:00:00

1193468128

Q

The number of milliseconds that have elapsed since 00:00:00

1193468128984


Test code

Public static void main (String [] args) {Date date = new Date (); // Use System for H. out. printf ("hour in the 24-digit format (0 before 2 digits): % tH % n", date); // System for I. out. printf ("hour in 12-digit format (0 before less than 2 digits): % tI % n", date); // Use System for k. out. printf ("hour in the 24-digit format (0 is not added before): % tk % n", date); // l uses System. out. printf ("two-digit 12-hour (not supplemented with 0): % tl % n", date); // Use System for M. out. printf ("2-digit minute (0 before less than 2 digits): % tM % n", date); // Use System for S. out. printf ("seconds with two digits (0 before less than two digits): % tS % n", date); // L uses System. out. printf ("3-digit Millisecond (0 before 3): % tL % n", date); // Use System for N. out. printf ("9-digit millisecond count (0 before 9 digits): % tN % n", date); // use String str = String for p. format (Locale. US, "morning or afternoon signs of lowercase letters (English): % tp", date); System. out. println (str); System. out. printf ("lowercase letter in the morning or afternoon Mark (medium): % tp % n", date); // Use System for z. out. printf ("offset of RFC822 Time Zone relative to GMT: % tz % n", date); // Use System for Z. out. printf ("Time Zone abbreviation string: % tZ % n", date); // use System. out. printf ("the number of seconds since 00:00:00: % ts % n", date); // Use System for Q. out. printf ("the number of milliseconds that have elapsed since 00:00:00: % tQ % n", date );}

Output result

Hour in the 24-digit format (0 for less than two digits): hour in the 12-digit format of 11 two digits (0 for less than two digits ): 11 hours in 24-digit notation (0 is not added before): hours in 12-digit notation (0 is not added before ): 11 minutes of two digits (0 for less than two digits): 03 seconds for two digits (0 for less than two digits ): the millisecond value of a 52-digit number (0 after 3 digits): the millisecond value of a 773-digit number (0 after 9 digits ): 773000000 morning or afternoon Mark of lowercase letters (English): am morning or afternoon Mark of lowercase letters (medium): Offset of morning to RFC822 Time Zone of GMT: + 0800 Time Zone abbreviation string: number of seconds passed by CST 00:00:00 to the present: 1347246232 00:00:00 to the present: 1347246232773

Reprinted from: Use of JAVA String formatting-String. format ()

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.