In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use Java format string", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Java format string" article.
Class Formatter
This class provides support for layout adjustment and alignment, common formats for numeric, string, and date / time data, and local-specific output. Common Java types such as byte, BigDecimal, and Calendar are supported. Provide limited formatting customization for any user type through the Formattable interface.
The formatter is not necessarily safe for multithreaded access. Thread safety is optional.
Examples are as follows:
StringBuilder sb = new StringBuilder (); / / Send all output to the Appendable object sb Formatter formatter = new Formatter (sb, Locale.US); / / shows that the parameter index can be used to reorder the output. Formatter.format ("4 $2s% 3 $2s% 2 $2s% 1 $2s", "a", "b", "c", "d") / / > "d c b a" / / the locale optional as the first parameter can be used to obtain locale-specific numeric formats. Precision and width can be given for rounding and alignment of values. Formatter.format (Locale.FRANCE, "e =% + 10.4f", Math.E); / /-> "e = + 2meme7183" / /'('numeric flags can be used to format negative numbers with parentheses instead of minus signs. The group delimiter is automatically inserted. Formatter.format ("Amount gained or lost since last statement: $% (, .2f", balanceDelta); / /-> "Amount gained or lost since last statement: $(6217.58)"
As shown in the following call, there are convenient methods for common formatting requests:
/ / write a formatted string to System.out. System.out.format ("Local time:% tT", Calendar.getInstance ()); / /-> "Local time: 13:34:18" / / writes the formatted output to System.err. System.err.printf ("Unable to open file'% 1$ slots:% 2$ s", fileName, exception.getMessage ()); / /-> "Unable to open file 'food': No such file or directory"
Like sprintf (3) in C, strings can be formatted in String.format using static methods:
/ / format a string containing the date import java.util.Calendar; import java.util.GregorianCalendar; import static java.util.Calendar.*; Calendar c = new GregorianCalendar (1995, MAY, 23); String s = String.format ("Duke's Birthday:% 1$ tb% 1$ te,% 1$ tY", c); / /-> s = = "Duke's Birthday: May 23, 1995"
* each method that produces formatted output requires a format string and a parameter list.
A format string is a string that an String may contain fixed text and one or more embedded format specifiers.
Https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#syntax
Calendar c =...; String s = String.format ("Duke's Birthday:% 1$ tm% 1$ te,%1$ tY", c)
This format string is the first parameter of the format method. It contains three format specifiers'1$ tm','1$ te', and'1$ tY 'that indicate how parameters should be handled and where they should be inserted in the text. The rest of the format string is the fixed text "Dukes Birthday:", including any other spaces or punctuation. The parameter list contains all parameters passed to the method after the format string.
Format specifiers for generic, character, and numeric types have the following syntax:
% [argument_index$] [flags] [width] [.clients] conversion
The optional argument_index is a decimal integer that represents the position of the parameter in the parameter list. The first parameter is referenced with "1 $", the second with "2 $", and so on.
The optional flags is a set of characters used to modify the output format. The collection of valid flags depends on the case of the transformation.
The optional width is a positive decimal integer that represents the minimum number of characters to write to the output.
The optional precision is a non-negative decimal integer that is usually used to limit the number of characters. The specific behavior depends on the situation of the transformation.
The required conversion is a character indicating how the parameter should be formatted. The valid transform set for a given parameter depends on the data type of the parameter.
The format specifier used to represent the type of date and time has the following syntax:
% [argument_index$] [flags] [width] conversion
The optional argument_index, flags, and width definitions are as above.
The required conversion is a sequence of two characters. The first character is't'or'T'. The second character represents the format to be used. These characters are similar to but not exactly the same as those defined by GNUdate and POSIXstrftime (3c).
Format specifiers that do not correspond to parameters have the following syntax:
% [flags] [width] conversion
The optional flags and width definitions are as above.
The required conversion is a character that indicates what to insert in the output.
Conversion
Conversion is divided into the following categories:
General-can be applied to any parameter type
Characters-can be applied to the basic types that represent Unicode characters: char, Character, byte, Byte, short and Short. This conversion can also be applied to types int and Integer when Character.isValidCodePoint (int) returns true
Figures
Integral-can be applied to Java integer types: byte, Byte, short, Short, intand Integer, long, Long, and BigInteger (but not char or Character)
Floating-can be applied to Java floating-point types: float, Float, double, Double, and BigDecimal
Date / time-can be applied to Java types that can encode a date or time: long, Long, Calendar and DateTemporalAccessor
Percentage-produces literal'%'('u0025')
Line delimiters-generate platform-specific row delimiters
For category General, Character, Numberic, Integral, and Date/Time transformations, unless otherwise noted, the result is "null" if the parameter arg is null.
The conversion parameter category describes' baked recently written B'. Generally, if the parameter arg is null, then the result is "false". If arg is aboolean or Boolean, the result is determined by. The returned string String.valueOf (arg). Otherwise, the result is "true".' The general result is Integer.toHexString (arg.hashCode ()) obtained through the call. Generally, if arg implements Formattable, then arg.formatTo is called. Otherwise, the result arg.toString () is obtained by calling.' The result is a Unicode character'd' integer, the result is formatted as a decimal integer'o' integer, the result is formatted as an octal integer, the result is formatted as a hexadecimal integer, the floating point result is formatted as the decimal number'f'in computer science notation, and the floating point result is formatted as a decimal number'g'. 'G' floating-point results are formatted using computer science notation or decimal format It depends on the precision and the rounded value.' The floating-point result is formatted as a hexadecimal floating-point number with significant numbers and exponents. Although the latter belongs to the floating-point parameter category, this type does not support this conversion. BigDecimal't','T' date / time prefix for date and time conversion characters. See date / time conversion.' % 'percent result is literal'%'('\ u0025')'n' line delimiter result is platform-specific row delimiter
Date / time conversion
The following date and time conversion suffix characters are defined for the two conversion.
The following conversion characters are used for formatting time:
Hours of the day in the hourly system of 24 hours, in double-digit format, with leading zeros if necessary, that is, 00-23.'I'12 hours, with leading zeros if necessary, that is, 01-12.'k'24 hours of the day, that is, 0-23.'l'12 hours, that is, 1-12 hours M' hours, with leading zeros if necessary. That is, the number of seconds in 00-59.seconds S' minutes, formatted as two digits with leading zeros if necessary, that is, 00-60 ("60" is a special value required to support leap seconds).' Milliseconds within L'seconds are in three-digit format, with leading zeros if necessary, nine-digit format and leading zeros if necessary, that is, locale-specific morning or afternoon am marks in lowercase 000000000-999999999.millip', such as "" or "pm". Using the conversion prefix'T 'forces this output to be uppercase.' Z'RFC 822 style numeric time zone is offset from GMT, for example,-0800. This value will be adjusted according to the needs of daylight saving time. The time zone used for long, Long, and Date is the default time zone for this Java virtual machine instance.' Z' represents a string of abbreviations for the time zone. This value will be adjusted according to the needs of daylight saving time. The time zone used for long, Long, and Date is the default time zone for this Java virtual machine instance. The locale of the formatter will replace the locale of the parameter, if any.' S 00:00:00 since the beginning of the era beginning on January 1, 1970, that is, the milliseconds from Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000.'Q' since the beginning of the era began on January 1, 1970, 00:00:00, that is. Long.MIN_VALUELong.MAX_VALUE
The following conversion characters are used to format the date:
The locale-specific full month name "January", such as "February". "b" is locale-specific abbreviated month name "Jan", such as "Feb".'h' and the same'b'. A locale-specific full name of a locale-specific day of the week, such as "Sunday", "Monday"a" short name of a locale-specific day of the week, such as "Sun", "Mon"C" four-digit year divided by 100, in a two-digit format, leading zero if necessary, that is, 00-99 years Y', in the format of at least four digits, with leading zeros if necessary, for example, for the Gregorian calendar, 0092 equals CE. The last two digits of the 92 calendar 'year are formatted with leading zeros if necessary, that is, one day of the year, 00-99.millij', is formatted as three digits with leading zeros, such as 001-366 Gregorian calendar, as needed. M'month, in two-digit format, with leading zeros if necessary, that is, 01-13.imperd' dates, in double-digit format, and if necessary, with leading zeros, that is, 01-31 dates, in double-digit format.
The following conversion characters are used to format common date / time combinations.
'The time format of the ringing 24-hour system is'% tH:%tM''the time format of the ringing 24-hour system is'% tH:%tM:%tS'. The time format of the ringing 12-hour system is'% tI:%tM:%tS% Tp'. The location of the morning or afternoon mark ('% Tp') may depend on the locale.' D 'date format is'% tm/%td/%ty'. 'F'ISO 8601 full date format is'% tY-%tm-%td'.'c 'date and time format is'% ta% tb% td% tT% tZ% tY', for example, 'Sun Jul 20 16:17:00 EDT 1969'.
Any character that is not explicitly defined as a date / time conversion suffix is illegal and reserved for future extensions.
Flags
Flags general character integer floating point date / time description'-'is that the result will be left-aligned.' # 'Yes 1-Yes 3-the result should use a transformation-dependent alternative form' +'- Yes 4-the result will always contain a flag'- Yes 4-the result will include a leading space of positive values, '0percent color-yes-the result will be filled with zero','- 2 is 5-the result will include the locale-specific component separator'('- yes 4, 5-the result will be enclosed in parentheses with negative numbers
1 depends on the definition of Formattable.
2 for 'd'conversion only.
3 is only applicable to'o','x' and 'X'conversion.
4 for 'dudes, BigInteger, Integer,long, and Long, and' X'conversions is applied to BigInteger or'd', to byte,Byte,short,Short,int and Integer,long.
5 applies only to'e','E','f','g' and 'G'conversion.
Any character that is not explicitly defined as a flag is illegal and reserved for future extensions.
The above is about the content of this article on "how to use Java format string". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.