JDK: java.util.Formatter.Conversion 类:
private static class Conversion {
// Byte, Short, Integer, Long, BigInteger
// (and associated primitives due to autoboxing)
static final char DECIMAL_INTEGER = 'd';
static final char OCTAL_INTEGER = 'o';
static final char HEXADECIMAL_INTEGER = 'x';
static final char HEXADECIMAL_INTEGER_UPPER = 'X';
// Float, Double, BigDecimal
// (and associated primitives due to autoboxing)
static final char SCIENTIFIC = 'e';
static final char SCIENTIFIC_UPPER = 'E';
static final char GENERAL = 'g';
static final char GENERAL_UPPER = 'G';
static final char DECIMAL_FLOAT = 'f';
static final char HEXADECIMAL_FLOAT = 'a';
static final char HEXADECIMAL_FLOAT_UPPER = 'A';
// Character, Byte, Short, Integer
// (and associated primitives due to autoboxing)
static final char CHARACTER = 'c';
static final char CHARACTER_UPPER = 'C';
// java.util.Date, java.util.Calendar, long
static final char DATE_TIME = 't';
static final char DATE_TIME_UPPER = 'T';
// if (arg.TYPE != boolean) return boolean
// if (arg != null) return true; else return false;
static final char BOOLEAN = 'b';
static final char BOOLEAN_UPPER = 'B';
// if (arg instanceof Formattable) arg.formatTo()
// else arg.toString();
static final char STRING = 's';
static final char STRING_UPPER = 'S';
// arg.hashCode()
static final char HASHCODE = 'h';
static final char HASHCODE_UPPER = 'H';
static final char LINE_SEPARATOR = 'n';
static final char PERCENT_SIGN = '%';
static boolean isValid(char c) {
return (isGeneral(c) || isInteger(c) || isFloat(c) || isText(c)
|| c == 't' || isCharacter(c));
}
格式化代码:
public class TestString {
public static void main(String[] args) {
String repStr = "hello";
int repInt = 33;
double repNo = 21.22;
String configStr = "%s world %d %f";
String ret = String.format(configStr, repStr,repInt,repNo);
System.out.println(ret);
}
}
JDK HELP:
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(). 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 invoking Integer.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
'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
Any characters not explicitly defined as conversions are illegal and are reserved for future extensions.
Date/Time Conversions
The following date and time conversion suffix characters are defined for the 't' and 'T' conversions. The types are similar to but not completely identical to those defined by GNU date and POSIX strftime(3c). Additional conversion types are provided to access Java-specific functionality (e.g. 'L' for milliseconds within the second).
The following conversion characters are used for formatting 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.
'Z' A string representing the abbreviation for the time zone. 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.
The following conversion characters are used for formatting 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.
The following conversion characters are used for formatting common date/time compositions. '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".
Any characters not explicitly defined as date/time conversion suffixes are illegal and are reserved for future extensions