日期格式的月和日固定长度? [英] Fixed length of month and day in date format?

查看:121
本文介绍了日期格式的月和日固定长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将Date对象格式化为固定长度的日期和月份,以便在列中具有良好的对齐方式?
例如:

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

而不是


15 May 2010
10 January 2010

谢谢!

推荐答案

一个格式方法与String相同的href =http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html> java.util.Formatter 类。 format(...)和类似于System.out.printf。

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

例如:

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

这篇关于日期格式的月和日固定长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆