JAVA string formatting the use of String.format of

  • 2020-05-17 05:36:09
  • OfStack

This article describes the use of JAVA string formatting - String.format (), as follows:

Formatting for general types

The format() method of the String class is used to create formatted strings and concatenate multiple string objects. Those of you familiar with C should remember the sprintf() method for C, which is similar. The format() method has two overloads.

format (String format Object... args) the new string USES the local language environment to make the string format and parameters to generate a formatted new string.

format(Locale locale, String format, Object... args) USES the specified locale to format the string and parameters to generate a formatted string.

Displays different converters to implement the conversion of different data types to strings, as shown in the figure.

转  换  符

说    明 

示    例

%s

字符串类型

"mingrisoft"

%c

字符类型

'm'

%b

布尔类型

true

%d

整数类型(10进制)

99

%x

整数类型(106进制)

FF

%o

整数类型(8进制)

77

%f

浮点类型

99.99

%a

106进制浮点类型

FF.35AE

%e

指数类型

9.38e+5

%g

通用浮点类型(f和e类型中较短的)

 

%h

散列码

 

%%

百分比类型

%n

换行符

 

%tx

日期与时间类型(x代表不同的日期与时间转换符

     

The test case


public static void main(String[] args) { 
  String str=null; 
  str=Stringformat("Hi,%s", " Wang li "); 
  Systemoutprintln(str); 
  str=Stringformat("Hi,%s:%s%s", " Wang Na "," Wang li "," The king chang ");      
  Systemoutprintln(str);              
  Systemoutprintf(" The letter a The upper case is: %c %n", 'A'); 
  Systemoutprintf("3>7 The results are: %b %n", 3>7); 
  Systemoutprintf("100 the 1 Half is: %d %n", 100/2); 
  Systemoutprintf("100 the 16 The base number is: %x %n", 100); 
  Systemoutprintf("100 the 8 The base number is: %o %n", 100); 
  Systemoutprintf("50 Yuan's book 5 Discount is: %f  yuan %n", 50*85); 
  Systemoutprintf(" overpriced 16 The base number is: %a %n", 50*85); 
  Systemoutprintf(" The price index above says: %e %n", 50*85); 
  Systemoutprintf(" The shorter length of the exponents and floating point results of the price above are: %g %n", 50*85); 
  Systemoutprintf(" The discount is %d%% %n", 85); 
  Systemoutprintf(" The letter A The hash code is: %h %n", 'A'); 
} 

The output


Hi, Wang li  
Hi, Wang Na : Wang Liwang zhang  
 The letter a The upper case is: A  
3>7 The results are: false  
100 the 1 Half is: 50  
100 the 16 The base number is: 64  
100 the 8 The base number is: 144  
50 Yuan's book 5 Discount is: 500000  yuan  
 overpriced 16 The base number is: 0x54p5  
 The price index above says: 250000e+01  
 The shorter length of the exponents and floating point results of the price above are: 5000  
 The discount is 85%  
 The letter A The hash code is: 41 

The flag that matches the converter, as shown in the figure.

标    志 说    明 示例 结果
+ 为正数或者负数添加符号 ("%+d",15) +15
- 左对齐 ("%-5d",15) |15  |
0 数字前面补0 ("%04d", 99) 0099
空格 在整数之前添加指定数量的空格 ("% 4d", 99) |  99|
, 以“,”对数字分组 ("%,f", 9999.99) 9,999.990000
( 使用括号包含负数 ("%(f", -99.99) (99.990000)
# 如果是浮点数则包含小数点,如果是16进制或8进制则添加0x或0 ("%#x", 99) ("%#o", 99)
格式化前1个转换符所描述的参数 ("%f和%<3.2f", 99.45) 99.450000和99.45
$ 被格式化的参数索引 ("%1$d,%2$s", 99,"abc") 99,abc

The test case


public static void main(String[] args) { 
  String str=null; 
  //$ use  
  str=Stringformat(" Format parameters $ The use of: %1$d,%2$s", 99,"abc");       
  Systemoutprintln(str);            
  //+ use  
  Systemoutprintf(" Symbols showing positive and negative Numbers: %+d with %d%n", 99,-99); 
  // fill O use  
  Systemoutprintf(" The best number is: %03d%n", 7); 
  // Space use  
  Systemoutprintf("Tab The key effect is: % 8d%n", 7); 
  // use  
  Systemoutprintf(" The effect of integer grouping is: %,d%n", 9989997); 
  // Number of Spaces and behind the decimal point  
  Systemoutprintf("1 The price of the book is: % 5f yuan %n", 8); 
} 

The output


 Format parameters $ The use of: 99,abc 
 Symbols showing positive and negative Numbers: +99 with -99 
 The best number is: 007 
Tab The key effect is:     7 
 The effect of integer grouping is: 9,989,997 
1 The price of the book is: 80000 yuan  

Date and event strings are formatted

In the interface of the program often need to display the time and date, but its display format is often unsatisfactory, need to write a lot of code through various algorithms to get the ideal date and time format. The %tx conversion is not described in detail in the string format, but is specifically used to format dates and times. The x in the %tx converter represents additional converters that process date and time formats, which can be combined to format dates and times into multiple formats.

A common format for combining dates and times, as shown in the figure.

转  换  符 说    明 示    例
c 包括全部日期和时间信息 星期6 10月 27 14:21:20 CST 2007
F “年-月-日”格式 2007-10-27
D “月/日/年”格式 10/27/07
r “HH:MM:SS PM”格式(12时制) 02:25:51 下午
T “HH:MM:SS”格式(24时制) 14:28:16
R “HH:MM”格式(24时制) 14:28

The test case


public static void main(String[] args) { 
  Date date=new Date();                 
  //c The use of  
  Systemoutprintf(" Full date and time information: %tc%n",date);     
  //f The use of  
  Systemoutprintf(" years - month - Format: %tF%n",date); 
  //d The use of  
  Systemoutprintf(" month / day / Format: %tD%n",date); 
  //r The use of  
  Systemoutprintf("HH:MM:SS PM Format ( 12 When the system) : %tr%n",date); 
  //t The use of  
  Systemoutprintf("HH:MM:SS Format ( 24 When the system) : %tT%n",date); 
  //R The use of  
  Systemoutprintf("HH:MM Format ( 24 When the system) : %tR",date); 
} 

The output


 Full date and time information: week 1 9 month  10 10:43:36 CST 2012 
 years - month - Format: 2012-09-10 
 month / day / Format: 09/10/12 
HH:MM:SS PM Format ( 12 When the system) : 10:43:36  In the morning  
HH:MM:SS Format ( 24 When the system) : 10:43:36 
HH:MM Format ( 24 When the system) : 10:43 

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


public static void main(String[] args) { 
  Date date=new Date();                   
  //b Month is short for month  
  String str=Stringformat(LocaleUS," English month abbreviation: %tb",date);    
  Systemoutprintln(str);                                       
  Systemoutprintf(" Local month abbreviation: %tb%n",date); 
  //B The full name of the month  
  str=Stringformat(LocaleUS," Full name of month: %tB",date); 
  Systemoutprintln(str); 
  Systemoutprintf(" Full name of local month: %tB%n",date); 
  //a Week is short for week  
  str=Stringformat(LocaleUS," English week abbreviation: %ta",date); 
  Systemoutprintln(str); 
  //A The full name of the week  
  Systemoutprintf(" Local week abbreviation: %tA%n",date); 
  //C The use of two years ago  
  Systemoutprintf(" The first two digits of the year 0 ) : %tC%n",date); 
  //y The use of two years after  
  Systemoutprintf(" The last two digits of the year 0 ) : %ty%n",date); 
  //j The use of 1 In the number of days  
  Systemoutprintf("1 Days of the year (days of the year) : %tj%n",date); 
  //m The use of months  
  Systemoutprintf(" A month with two digits 0 ) : %tm%n",date); 
  //d The use of day ( 2 Bit, not enough to zero)  
  Systemoutprintf(" A two-digit day is not enough for two digits 0 ) : %td%n",date); 
  //e The use of day ( 1 Bit not zeroing)  
  Systemoutprintf(" Days of the month 0 ) : %te",date); 
} 

The output


 English month abbreviation: Sep 
 Local month abbreviation: 9 month  
 Full name of month: September 
 Full name of local month: 9 month  
 English week abbreviation: Mon 
 Short for local week: week 1 
 The first two digits of the year 0 ) : 20 
 The last two digits of the year 0 ) : 12 
1 Days of the year (days of the year) : 254 
 A month with two digits 0 ) : 09 
 A two-digit day is not enough for two digits 0 ) : 10 
 Days of the month 0 ) : 10 

There are more and more precise time format converters than date format converters. It can format time into units such as hours, minutes, seconds, and even milliseconds. The converters for the formatted time string are shown in the figure.

转  换  符 说    明 示    例
H 2位数字24时制的小时(不足2位前面补0) 15
I 2位数字12时制的小时(不足2位前面补0) 03
k 2位数字24时制的小时(前面不补0) 15
l 2位数字12时制的小时(前面不补0) 3
M 2位数字的分钟(不足2位前面补0) 03
S 2位数字的秒(不足2位前面补0) 09
L 3位数字的毫秒(不足3位前面补0) 015
N 9位数字的毫秒数(不足9位前面补0) 562000000
p 小写字母的上午或下午标记

中:下午

英:pm
 

z 相对于GMT的RFC822时区的偏移量 +0800
Z 时区缩写字符串 CST
s 1970-1-1 00:00:00 到现在所经过的秒数 1193468128
Q 1970-1-1 00:00:00 到现在所经过的毫秒数 1193468128984

The test code


public static void main(String[] args) { 
  Date date = new Date(); 
  //H The use of  
  Systemoutprintf("2 A digital 24 The hours are not enough 2 A front cover 0 ) :%tH%n", date); 
  //I The use of  
  Systemoutprintf("2 A digital 12 The hours are not enough 2 A front cover 0 ) :%tI%n", date); 
  //k The use of  
  Systemoutprintf("2 A digital 24 Hours on time (not made up in front) 0 ) :%tk%n", date); 
  //l The use of  
  Systemoutprintf("2 A digital 12 Hours on time (not made up in front) 0 ) :%tl%n", date); 
  //M The use of  
  Systemoutprintf("2 Bit number of minutes (not enough 2 A front cover 0 ) :%tM%n", date); 
  //S The use of  
  Systemoutprintf("2 Bit number of seconds 2 A front cover 0 ) :%tS%n", date); 
  //L The use of  
  Systemoutprintf("3 A bit number in milliseconds 3 A front cover 0 ) :%tL%n", date); 
  //N The use of  
  Systemoutprintf("9 The number of milliseconds of a bitwise digit 9 A front cover 0 ) :%tN%n", date); 
  //p The use of  
  String str = Stringformat(LocaleUS, " A morning or afternoon marker in lowercase letters ( The British ) : %tp", date); 
  Systemoutprintln(str);  
  Systemoutprintf(" Lower case morning or afternoon mark (middle) : %tp%n", date); 
  //z The use of  
  Systemoutprintf(" Relative to the GMT the RFC822 The offset of the time zone :%tz%n", date); 
  //Z The use of  
  Systemoutprintf(" Time zone abbreviation string :%tZ%n", date); 
  //s The use of  
  Systemoutprintf("1970-1-1 00:00:00  The number of seconds elapsed so far: %ts%n", date); 
  //Q The use of  
  Systemoutprintf("1970-1-1 00:00:00  The number of milliseconds to the present: %tQ%n", date); 
} 

The output


2 A digital 24 The hours are not enough 2 A front cover 0 ) :11 
2 A digital 12 The hours are not enough 2 A front cover 0 ) :11 
2 A digital 24 Hours on time (not made up in front) 0 ) :11 
2 A digital 12 Hours on time (not made up in front) 0 ) :11 
2 Bit number of minutes (not enough 2 A front cover 0 ) :03 
2 Bit number of seconds 2 A front cover 0 ) :52 
3 A bit number in milliseconds 3 A front cover 0 ) :773 
9 The number of milliseconds of a bitwise digit 9 A front cover 0 ) :773000000 
 A morning or afternoon marker in lowercase letters ( The British ) : am 
 Lowercase am or PM mark (middle) : am  
 Relative to the GMT the RFC822 The offset of the time zone :+0800 
 Time zone abbreviation string :CST 
1970-1-1 00:00:00  The number of seconds elapsed so far: 1347246232 
1970-1-1 00:00:00  The number of milliseconds to the present: 1347246232773 

Related articles: