Fixed month and day length in date format?

Is there a way to format a Date object for a fixed day and month length in order to have good alignment in the column? For instance:

  May 15, 2010
 January 10, 2010

Instead

  May 15, 2010
 January 10, 2010

Thanks!

+4
source share
1 answer

Look at the java.util.Formatter class, the format of which is the same as the String.format (...) format and is similar to System.out. Printf.

For instance:

import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class FormatDateCalendar { public static final String FORMAT_STRING = "%1$-3td %1$-9tB %1$tY"; public static void main(String[] args) { Calendar c1 = new GregorianCalendar(2011, Calendar.FEBRUARY, 3); Calendar c2 = new GregorianCalendar(2010, Calendar.MAY, 15); Date today = new Date(); System.out.printf(FORMAT_STRING + "%n", c1); System.out.printf(FORMAT_STRING + "%n", c2); System.out.printf(FORMAT_STRING + "%n", today); } } 
+6
source

All Articles