Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

Posts Tagged ‘formatter

Java Output Formatting

leave a comment »

Here are some often used java output formatting with using String formatter..

// format string output
System.out.println(String.format("String  [%s]", "a string"));
/* $> String  [a string] */

// format integer output
System.out.println(String.format("Integer [%d]", 12345));
/* $> Integer [12345] */

// format double output
System.out.println(String.format("Double  [%.2f]", 123.456d));
/* $> Double  [123.46] */

// format floating point output
System.out.println(String.format("Float   [%.3f]", 0.123f));
/* $> Float   [0.123] */

// format boolean output
System.out.println(String.format("Boolean [%b]", true));
/* $> Boolean [true] */

// format date output
System.out.println(String.format(
"SimpleDateFormat-1 [%s]", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())));
/* $> SimpleDateFormat-1 [2008-10-20 14:36:50.234] */

// format date output
System.out.println(String.format(
"SimpleDateFormat-2 [%s]", new SimpleDateFormat("yyMMdd_HHmmss").format(new Date())));
/* $> SimpleDateFormat-2 [081020_143650] */

Written by Alfred

October 20, 2008 at 14:44

Posted in Java

Tagged with ,