Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

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] */
Advertisements

Written by Alfred

October 20, 2008 at 14:44

Posted in Java

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: