MessageFormat formatting examples

A 2009 post from CoffeaElectronica.com blog with numerous examples of formatting numbers, times and dates with MessageFormat.format().

Here's the code example. You'll have to go to the post to see the output!

Object[] params = new Object[]{ new Date() };
out.println("Dates:");
out.println("  default:\t" + format("{0}",                 params) );
out.println("  short:\t"   + format("{0,date,short}",      params) );
out.println("  medium:\t"  + format("{0,date,medium}",     params) );
out.println("  long:\t\t"  + format("{0,date,long}",       params) );
out.println("  custom:\t"  + format("{0,date,d MMMM yyyy}",params) );

out.println("\nTime:");
out.println("  default:\t" + format("{0,time}",         params) );
out.println("  short:\t"   + format("{0,time,short}",   params) );
out.println("  medium:\t"  + format("{0,time,medium}",  params) );
out.println("  long:\t\t"  + format("{0,time,long}",    params) );
out.println("  full:\t\t"  + format("{0,time,full}",    params) );
out.println("  custom:\t"  + format("{0,time,HH:mm:ss}",params) );

params = new Object[]{ new Float(31415.967F) };
out.println("\nNumbers:");
out.println("  default:\t"  + format("{0}",                  params) );
out.println("  integer:\t"  + format("{0,number,integer}",   params) );
out.println("  currency:\t" + format("{0,number,currency}",  params) );
out.println("  percent:\t"  + format("{0,number,percent}",   params) );
out.println("  custom:\t"   + format("{0,number,#,###.0000}",params) );
Posted April 05, 2017 05:21 PM EDT

More Like This Post