String Format格式化

String Format格式化

一、格式说明

1.通用字符和数字格式

%[argument_index$][flags][width][.precision]conversion

argument_index:指定参数的位置,第一个参数1$....以此类推

flags:输出参数的格式

width:表示要输出的最小字符数

precision:确定输出的精度,具体的行为取决于转换

conversion:指示参数应该如何格式化

2.日期和时间格式

%[argument_index$][flags][width]conversion

所需的转换是两个字符的序列。第一个字符是't'或'T'。第二个字符表示要使用的格式。

3.与参数不对应的格式

%[flags][width]conversion

二、conversion转换支持的类型

1.General :可以运用于任何类型

2.Character :char/Character/byte/Byte/short/Short。当Character.isValidCodePoint(int)返回true的时候可以格式int/Integer

3.Numeric:数值

3.1Integral :整型可以用于byte/Byte/short/Short/int/Integer/long/Long/BigInteger,但是不可以用于char或者Character

3.1Floating Point:浮点数可以运用于float/Float/double/Double/BigDecimal

4.Date/Time:时间日期可以运用于long/Long/Calendar/Date/TemporalAccessor

5.Percent :百分比会生成一个char'%'

6.Line Separator:生成符合平台的行分隔符

三、Argument 参数

1.Conversions

以下的参数格式中,'B', 'H', 'S', 'C', 'X', 'E', 'G', 'A', 'T'和小写的作用一样,但是会将输出转换为大写,类似于String.toUpperCase()

Conversion

Argument Category

Description

'b''B'

general

If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true".

'h''H'

general

If the argument arg is null, then the result is "null". Otherwise, the result is obtained by invokingInteger.toHexString(arg.hashCode()).

's''S'

general

If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString().

'c''C'

character

The result is a Unicode character

'd'

integral

The result is formatted as a decimal integer

'o'

integral

The result is formatted as an octal integer

'x''X'

integral

The result is formatted as a hexadecimal integer

'e''E'

floating point

The result is formatted as a decimal number in computerized scientific notation

'f'

floating point

The result is formatted as a decimal number

'g''G'

floating point

The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding.

'a''A'

floating point

The result is formatted as a hexadecimal floating-point number with a significand and an exponent. This conversion is notsupported for the BigDecimal type despite the latter's being in the floating point argument category.

't''T'

date/time

Prefix for date and time conversion characters. See Date/Time Conversions.

'%'

percent

The result is a literal '%' ('\u0025')

'n'

line separator

The result is the platform-specific line separator

2.Date/Time Conversions

2.1以下转换用于时间times格式

'H'

Hour of the day for the 24-hour clock, formatted as two digits with a leading zero as necessary i.e. 00 - 23.

'I'

Hour for the 12-hour clock, formatted as two digits with a leading zero as necessary, i.e. 01 - 12.

'k'

Hour of the day for the 24-hour clock, i.e. 0 - 23.

'l'

Hour for the 12-hour clock, i.e. 1 - 12.

'M'

Minute within the hour formatted as two digits with a leading zero as necessary, i.e. 00 - 59.

'S'

Seconds within the minute, formatted as two digits with a leading zero as necessary, i.e. 00 - 60 ("60" is a special value required to support leap seconds).

'L'

Millisecond within the second formatted as three digits with leading zeros as necessary, i.e. 000 - 999.

'N'

Nanosecond within the second, formatted as nine digits with leading zeros as necessary, i.e. 000000000 - 999999999.

'p'

Locale-specific morning or afternoon marker in lower case, e.g."am" or "pm". Use of the conversion prefix 'T' forces this output to upper case.

'z'

RFC 822 style numeric time zone offset from GMT, e.g. -0800. This value will be adjusted as necessary for Daylight Saving Time. For longLong, and Date the time zone used is the default time zone for this instance of the Java virtual machine.

'Z'

A string representing the abbreviation for the time zone. This value will be adjusted as necessary for Daylight Saving Time. For longLong, and Datethe time zone used is the default time zone for this instance of the Java virtual machine. The Formatter's locale will supersede the locale of the argument (if any).

's'

Seconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000.

'Q'

Milliseconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE.

2.2以下转换用于日期dates格式

'B'

Locale-specific full month name, e.g. "January""February".

'b'

Locale-specific abbreviated month name, e.g. "Jan""Feb".

'h'

Same as 'b'.

'A'

Locale-specific full name of the day of the week, e.g. "Sunday""Monday"

'a'

Locale-specific short name of the day of the week, e.g. "Sun""Mon"

'C'

Four-digit year divided by 100, formatted as two digits with leading zero as necessary, i.e. 00 - 99

'Y'

Year, formatted as at least four digits with leading zeros as necessary, e.g. 0092 equals 92 CE for the Gregorian calendar.

'y'

Last two digits of the year, formatted with leading zeros as necessary, i.e. 00 - 99.

'j'

Day of year, formatted as three digits with leading zeros as necessary, e.g. 001 - 366 for the Gregorian calendar.

'm'

Month, formatted as two digits with leading zeros as necessary, i.e. 01 - 13.

'd'

Day of month, formatted as two digits with leading zeros as necessary, i.e. 01 - 31

'e'

Day of month, formatted as two digits, i.e. 1 - 31.

2.3以下转换用于日期/时间date/time格式

'R'

Time formatted for the 24-hour clock as "%tH:%tM"

'T'

Time formatted for the 24-hour clock as "%tH:%tM:%tS".

'r'

Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp". The location of the morning or afternoon marker ('%Tp') may be locale-dependent.

'D'

Date formatted as "%tm/%td/%ty".

'F'

ISO 8601 complete date formatted as "%tY-%tm-%td".

'c'

Date and time formatted as "%ta %tb %td %tT %tZ %tY", e.g. "Sun Jul 20 16:17:00 EDT 1969".

四、Flags

y表示是否支持指定的类型

Flag

General

Character

Integral

Floating Point

Date/Time

Description

'-'

y

y

y

y

y

The result will be left-justified.

'#'

y1

-

y3

y

-

The result should use a conversion-dependent alternate form

'+'

-

-

y4

y

-

The result will always include a sign

'  '

-

-

y4

y

-

The result will include a leading space for positive values

'0'

-

-

y

y

-

The result will be zero-padded

','

-

-

y2

y5

-

The result will include locale-specific grouping separators

'('

-

-

y4

y5

-

The result will enclose negative numbers in parentheses

1 Depends on the definition of Formattable.

2 For 'd' conversion only.

3 For 'o''x', and 'X' conversions only.

4 For 'd''o''x', and 'X' conversions applied to BigInteger or 'd' applied to byteByteshortShortint and Integerlong, and Long.s

5 For 'e''E''f''g', and 'G' conversions only.

五、Width

表示要输出的最小字符数

六、Precision

对于一般参数类型,precision是要写入输出的最大字符数。

对于浮点转换'a','A','e','E'和'f',精度是小数点后的位数。如果转换为“g”或“G”,则精度是舍入后得到的幅度中的总位数。

对于字符,整数和日期/时间参数类型以及百分比和行分隔符转换,精度不适用;如果提供精度,则抛出异常。

七、Argument Indexs

参数索引是一个十进制整数,指示参数在参数列表中的位置。1$表示第一个参数。

八、常用格式

//整数类型,包含分隔符

System.out.println(String.format("[%,d]", -2000000000));

//字符串类型

System.out.println(String.format("[%s]", "string"));

//浮点数类型(四舍五入为2为小数)

System.out.println(String.format("[%1$.2f],[%1$(.2f],[%1$f]", -26.3596));

//boolean类型

System.out.println(String.format("[%b],[%b],[%b],[%b],[%b]", true, null, "456789", "", "false"));

//百分比输出%后面使用%会原样输出

System.out.println(String.format("%.2f%%", 56.369));

//日期时间类型,格式前面必须t

System.out.println(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS:%1$tL", LocalDateTime.now()));

//将byte, Byte, short, Short, int, Integer的char对应的值

System.out.println(String.format("[%c]", 97));

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie
应支付0元
点击重新获取
扫码支付

支付成功即可阅读