Java string Format-string.format () use _java

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

This article describes the use of Java string Format-string.format (), as follows:

Format of the general type

The format () method of the string class is used to create a formatted string and to concatenate multiple string objects. Familiar with C language students should remember the C language sprintf () method, the two have a similar place. There are two overloaded forms of the format () method.

Format (string format, Object ... args) The new string uses the local locale, making a string format and a new string for the parameter generation format.

Format (Locale Locale, string format, Object ... args) uses the specified locale to make string formatting and parameter generation formatted strings.

Shows different conversions that implement different data types to strings, as shown in the figure.

Conversion characters

Description

Example

%s

String type

"Mingrisoft"

%c

Character type

' m '

%b

Boolean type

True

%d

Integer type (decimal)

99

%x

Integer type (hexadecimal)

Ff

%o

Integer type (octal)

77

%f

Floating-point types

99.99

%a

Hexadecimal floating-point type

Ff.35ae

%e

Exponential type

9.38e+5

%g

Generic floating-point types (shorter in types F and E)

%h

Hash code

%%

Percent type

%n

Line feed

%tx

Date and Time type (x represents a different date and time conversion character

Test Cases

public static void Main (string[] args) { 
  String str=null; 
  Str=stringformat ("hi,%s", "Wang Li"); 
  Systemoutprintln (str); 
  Str=stringformat ("hi,%s:%s%s", "Wang Nan", "Wang Li", "King Zhang");      
  Systemoutprintln (str);              
  systemoutprintf ("Capital of the letter A is:%c%n", ' a '); 
  systemoutprintf ("3>7 results are:%b%n", 3>7); 
  systemoutprintf ("Half of 100 is:%d%n", 100/2); 
  systemoutprintf ("100 of the 16 number is:%x%n",); 
  systemoutprintf ("100 of the 8 number is:%o%n",); 
  systemoutprintf ("50 yuan book hit 5 discount is:%f Yuan%n", 50*85); 
  systemoutprintf ("Above the price of 16 of the number is:%a%n", 50*85); 
  systemoutprintf ("Above the price index says:%e%n", 50*85); 
  systemoutprintf ("The index of the above price and the shorter length of the floating-point results are:%g%n", 50*85); 
  systemoutprintf ("The above discount is%d%%%n",); 
  systemoutprintf ("The hash code of the letter A is:%h%n", "a"); 

Output results

Hi, Wang Li 
hi, Wang nan: Wang Lizhang The 
letter A is: a  
3>7 The result is: false  
100 is half: 16 of 100 is:  
100 of the 8 number is: 144  
50 yuan book hit 5 discount is: 500000 Yuan 
above the price of the 16 number is: 0X54P5  
above Price index said: 250000e+01 above the  
price index and the length of the floating-point results is shorter: 5000  
The discount above is 85%.  

A flag to match the translator, as shown in the figure.

Sign Description Example Results
+ Add a symbol to a positive or negative number ("%+d", 15) +15
- Left-aligned ("%-5d", 15) |15 |
0 Number Front 0 ("%04D", 99) 0099
Space Add a specified number of spaces before an integer ("% 4d", 99) | 99|
, Group numbers by "," ("%,f", 9999.99) 9,999.990000
( Use parentheses to include negative numbers ("% (f",-99.99) (99.990000)
# If the floating-point number contains a decimal point, add 0x or 0 if it is 16 or 8 ("% #x", 99) ("% #o", 99)
< Format the parameters described by the previous conversion character ("%f and%<3.2f", 99.45) 99.450000 and 99.45
$ The index of the parameter being formatted ("%1$d,%2$s", "the ABC") 99,abc

Test Cases

public static void Main (string[] args) { 
  String str=null; 
  $ using 
  str=stringformat ("format parameter $ use:%1$d,%2$s", "" "," abc ");       
  Systemoutprintln (str);            
  + Use 
  systemoutprintf ("Show positive negative sign:%+d and%d%n", 99,-99); 
  Complement o use 
  systemoutprintf ("The Most cattle number is:%03d%n", 7); 
  Spaces using 
  systemoutprintf ("Tab key effect is:% 8d%n", 7); 
  Use 
  systemoutprintf ("Integer Grouping effect is:%,d%n", 9989997); 
  Space and the number after the decimal point 
  systemoutprintf ("A book Price is:% 5f Yuan%n", 8); 

Output results

Use of the format parameter $: 99,ABC 
display positive negative sign: +99 and -99 the 
cow's number is: 007 
The effect of the TAB key is:    7 
integer Grouping effect is: 9,989,997 

Date and event strings formatted

In the program interface often need to display time and date, but its display is often unsatisfactory format, need to write a large number of code through a variety of algorithms to get the ideal date and time format. There is also a%tx converter in the string format that is specifically used to format dates and times, not described in detail. The x in the%tx converter represents a different format for processing dates and times, and their combination can format dates and times in multiple formats.

The format of the common date and time combinations, as shown in the figure.

Conversion characters Description Example
C Include full date and time information Saturday 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 (12 o'clock) 02:25:51 afternoon
T "HH:MM:SS" format (24 o'clock) 14:28:16
R "HH:MM" format (24 o'clock) 14:28

Test Cases

public static void Main (string[] args) { 
  date date=new date ();                 
  C's use 
  systemoutprintf ("All date and time information:%tc%n", date);     
  F's use 
  systemoutprintf ("year-month-day format:%tf%n", date); 
  D Use 
  systemoutprintf ("Month/day/year format:%td%n", date); 
  Use of R 
  systemoutprintf ("HH:MM:SS PM format (12 o'clock):%tr%n", date); 
  Use of T 
  systemoutprintf ("HH:MM:SS format (24 o'clock):%tt%n", date); 
  Use of R 
  systemoutprintf ("hh:mm format (24 o'clock):%tr", date); 

Output results

All date and time: Monday last 10:43:36 CST 
year-month-day format: 2012-09-10 
month/day/year format: 09/10/12 
HH:MM:SS pm format (12 o'clock): 10 : 43:36 a.m. 
HH:MM:SS Format (24 o'clock): 10:43:36 

A conversion character that defines a date format enables a date to generate a new string from a specified converter. These date converters are shown in the figure.

 public static void main (string[] args) {date date=new date ();    
  The use of B, the month abbreviation String str=stringformat (localeus, "English month Abbreviation:%TB", date);                                       
  Systemoutprintln (str); 
  systemoutprintf ("Local month abbreviation:%tb%n", date); 
  b use, the full name of the month Str=stringformat (Localeus, "English month Full name:%TB", date); 
  Systemoutprintln (str); 
  systemoutprintf ("Local Month full name:%tb%n", date); 
  The use of a, week abbreviation Str=stringformat (localeus, "abbreviation of the English Week:%ta", date); 
  Systemoutprintln (str); 
  The use of a, week full name systemoutprintf ("Local week's abbreviation:%ta%n", date); 
  The use of C, the first two-digit systemoutprintf ("the first two digits of the year (less than two front 0):%tc%n", date); 
  The use of Y, two digits after the year ("systemoutprintf two digits (less than two front 0):%ty%n", date); 
  Use of J, days of the Year systemoutprintf ("Days of the Year" (i.e. days of the year):%tj%n, date); 
  The use of M, month systemoutprintf ("Two-digit month (less than two front complement 0):%tm%n", date); 
  d The use of the day (two bits, not enough to fill 0) systemoutprintf ("Two digits of the day (less than two front fill 0):%td%n", date); 
Use of E, day (one does not make up 0) systemoutprintf ("Day of the Month (before 0):%te", date); } 

Output results

English month abbreviation: SEP 
Local month abbreviation: September 
English Month full name: September 
Local Month full name: September 
English week abbreviation: Mon 
Local week abbreviation: Monday 
The first two digits of the year (less than two front complement 0): 
the last two digits of the year (less than two digits ahead of 0) 
: Number of days in a year (that is, days of the year): 254 
Two-digit month (less than two front 0): 
Two-digit day (less than two front complement 0): 10 

The time format converter is more and more accurate than the date format conversion character. It can format time into units such as time, minutes, seconds, or even milliseconds. The conversion character for the format time string is shown in the figure.

Conversion characters Description Example
H 2-digit 24 o'clock hours (less than 2 front complement 0) 15
I 2-digit 12 o'clock hours (less than 2 front complement 0) 03
K 2-digit 24 o'clock hours (not up to 0 in front) 15
L 2-digit 12 o'clock hours (not up to 0 in front) 3
M 2-digit minute (less than 2-digit front complement 0) 03
S 2-digit seconds (less than 2-digit front complement 0) 09
L 3-digit millisecond (less than 3 digits ahead of 0) 015
N 9-digit number of milliseconds (less than 9 digits ahead of 0) 562000000
P Small letter in the morning or afternoon mark

Middle: PM

English: PM

Z Offset of the RFC822 time zone relative to GMT +0800
Z Time zone abbreviation string Cst
S The number of seconds elapsed from 1970-1-1 00:00:00 to the present 1193468128
Q The number of milliseconds elapsed from 1970-1-1 00:00:00 to the present 1193468128984

Test code

public static void Main (string[] args) {Date date = new Date (); 
  H's use of systemoutprintf ("2-digit 24 o'clock hours (less than 2 digits ahead of 0):%th%n", date); 
  The use of systemoutprintf ("2-digit 12 o'clock hours (less than 2 digits ahead of 0):%ti%n", date); 
  K is used systemoutprintf ("2 digits 24 o'clock hours (front not 0):%tk%n", date); 
  L Use systemoutprintf ("2 digits 12 o'clock hours (front not 0):%tl%n", date); 
  M's Use systemoutprintf ("2-digit minutes (less than 2 front complement 0):%tm%n", date); 
  s use systemoutprintf ("2-digit seconds (less than 2 digits ahead of 0):%ts%n", date); 
  L Uses systemoutprintf ("3-digit millisecond (less than 3 front complement 0):%tl%n", date); 
  N Uses systemoutprintf ("9-digit milliseconds (less than 9 digits ahead of 0):%tn%n", date); 
  P's use of String str = StringFormat (localeus, "small letter morning or afternoon mark (English):%TP", date);  
  Systemoutprintln (str); 
  systemoutprintf ("Small Letter of the morning or afternoon mark (medium):%tp%n", date); 
  Z's use systemoutprintf ("offset of RFC822 time zone relative to GMT:%tz%n", date); 
  Use of Z systemoutprintf ("time zone abbreviation string:%tz%n", date); 
  The use of S systemoutprintf ("the number of seconds elapsed from 1970-1-1 00:00:00 to now:%ts%n", date); Q's Use systemoutprintf ("The number of milliseconds elapsed from 1970-1-1 00:00:00 to now:%tq%n", DaTE); 

 }

Output results

2-digit 24 o'clock hours (less than 2 digits ahead of 0): one 
2 digit 12 o'clock hours (less than 2 front 0): 
2 digits 24 o'clock hours (front does not make up 0): one 
2 digit 12 o'clock hours (front does not fill 0): 
One 2-digit minute (less than 2-digit front complement 0): 
2 digit seconds (less than 2 digits ahead of 0): 
3-digit milliseconds (less than 3 digits ahead of 0): 773 
9-digit milliseconds (less than 9 front 0): 773000000 
Small letter of the morning or afternoon mark (English): AM 
Small letter in the morning or afternoon mark (medium): The morning 
offset of the RFC822 time zone relative to GMT: +0800 
Time zone abbreviation string: CST 
1970-1-1 The number of seconds elapsed from 00:00:00 to the present: 1347246232 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.