원문참조
http://docs.oracle.com/javase/tutorial/java/data/numberformat.html
http://www.xinotes.org/notes/note/1195/
https://gist.github.com/huljas/1122618
API 설명문서참조
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax
import java.util.Date;
public class Main {
public static void main(String args[]){
//simple example of formatted string in Java
//%d is used to format decimals like integers
//position of argument is the order in which they appear in source String
// e.g here 40021 will replace first %d and 3000 will replace second %d.
String formattedString = String.format("Order with OrdId : %d and Amount: %d is missing", 40021, 3000);
System.out.println(formattedString);
//Order with OrdId : 40021 and Amount: 3000 is missing
System.out.printf("Order with OrdId : %d and Amount: %d is missing \n", 40021, 3000);
//Order with OrdId : 40021 and Amount: 3000 is missing
//%s is used to denote String arguments in formatted String
String str = String.format("Hello %s", "World");
System.out.println(str);
//Hello World
//if argument is not convertible into specified data type than
//Formatter will throw following java.util.IllegalFormatConversionException
//e.g. specifying %d and passing 3.0
//str = String.format("Number %d", 3.0);
// Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
// at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
// at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709)
// at java.util.Formatter$FormatSpecifier.print(Formatter.java:2661)
// at java.util.Formatter.format(Formatter.java:2433)
// at java.util.Formatter.format(Formatter.java:2367)
// at java.lang.String.format(String.java:2769)
//common meta characters used in String.format() and
//System.out.printf() method: %s - String , %d - decimal integer
// %f - float %tD - date as MM/dd/yy while %td is day %tm is month
// and %ty is 2 digit year while %tY is four digit year
//Formatting date in String format method - date in MM/dd/yy
str = String.format("Today is %tD", new Date());
System.out.println(str);
//Today is 01/20/14
Date today = new Date();
System.out.printf("Date in dd/mm/yy format %td/%tm/%ty %n", today,today,today );
//Date in dd/mm/yy format 20/01/14
str = String.format("Today is %tF", today);
System.out.println(str);
//Today is 2014-01-20
// date as July 25, 2012, difference between %td and %te is that
// %td use leading zero while %te doesn't
System.out.printf("Today is %tB %te, %tY, %tF %n", today,today,today,today);
//Today is 1월 20, 2014, 2014-01-20
str = String.format("Current Time is %tT", new Date().getTime());
System.out.println(str);
//Current Time is 12:39:34
str = String.format("Date and Time %1$tH:%1$tM:%1$tS.%1$tL %1$tY.%1$tm.%1$td", new Date().getTime());
System.out.println(str);
//Date and Time 12:42:53.974 2014.01.21
//adding leading zero in numbers using String format,
//%d is for decimal, 8 specify formatted number should be 8 digit and 0 specify use
//leading zero, default is space, so if you don't specify leading
// character space will be used.
System.out.printf("Amount : %08d %n" , 221);
//Amount : 00000221
//printing positive and negative number using String format
//+ sign for positive, - for negative and %n is for new line
System.out.printf("positive number : +%d %n", 1534632142);
//positive number : +1534632142
System.out.printf("negative number : -%d %n", 989899);
//negative number : -989899
//printing floating point number with System.format()
System.out.printf("%f %n", Math.E);
//2.718282
//3 digit after decimal point
System.out.printf("%.3f %n", Math.E);
//2.718
//8 charcter in width and 3 digit after decimal point
System.out.printf("%8.3f %n", Math.E);
// 2.718
//adding comma into long numbers
System.out.printf("Total %,d messages processed today", 10000000);
//Total 10,000,000 messages processed today
}
}
Date conversion suffixes
Character Meaning
'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.
Time conversion suffixes
Character Meaning
'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.
Date/Time composite suffixes
Character Meaning
'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".
The flags
Flag Meaning
'-' The result will be left-justified. Default is right-justify, so there's no flag for right-justify.
'#' The result should use a conversion-dependent alternate form.
'+' The result will always include a sign (for numbers).
' ' The result will include a leading space for positive values (for numbers).
'0' The result will be zero-padded (for numbers).
',' The result will include locale-specific grouping separators (for numbers).
'(' The result will enclose negative numbers in parentheses.