Fixed length of month and day in date format?

谁说我不能喝 提交于 2019-12-07 08:22:16

问题


Is there any way to format Date object to made fixed length of Day and Month in order to have good alignment in a column? For example:

15 May      2010
10 January  2010

Instead of

15 May 2010
10 January 2010

Thanks!


回答1:


Have a look at the java.util.Formatter class whose format method is the same as String.format(...) and similar to System.out.printf.

For example:

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);
   }
}


来源:https://stackoverflow.com/questions/6515371/fixed-length-of-month-and-day-in-date-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!