Apache Commons の lang ライブラリを使って日付の出力を試す

サンプルとして使っているクラスは下記のもの。

DateUtils の parseDate の引数で指定する日付書式は、

を参照。

サンプルコード

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.time.DateFormatUtils;

public class TimeAndDate {
	public static void main(String args[]) {
		Date today = new Date();
		System.out.println("現在日時を出力");
		System.out.println(today + "\n");

		// See at http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/time/DateFormatUtils.html
		System.out.println("DateFormatUtils で現在日時を出力する");
		System.out.println(DateFormatUtils.ISO_DATE_FORMAT.format(today));
		System.out.println(DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(today));
		System.out.println(DateFormatUtils.ISO_DATETIME_FORMAT.format(today));
		System.out.println(DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(today));
		System.out.println(DateFormatUtils.ISO_TIME_FORMAT.format(today));
		System.out.println(DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(today));
		System.out.println(DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(today));
		System.out.println(DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(today));
		System.out.println(DateFormatUtils.SMTP_DATETIME_FORMAT.format(today));
		System.out.println("少しだけ工夫をしてみる");
		System.out.println(
				DateFormatUtils.ISO_DATE_FORMAT.format(today)
				+ " " +
				DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(today) + "\n");

		String date01 = "2011/04/01 13:32:09";
		Date parsedDate = null;
		try {
			parsedDate = DateUtils.parseDate(date01, new String[]{"yyyy-MM-dd", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss"});
			System.out.println("yyyy/MM/dd HH:mm:ss をパースして出力してみる");
			System.out.println(date01);
			System.out.println(parsedDate);
			System.out.println(DateFormatUtils.ISO_DATE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_DATETIME_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_TIME_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.SMTP_DATETIME_FORMAT.format(parsedDate) + "\n");
		} catch (ParseException e) {
			e.printStackTrace();
		}

		String date02 = "2011-04-02";
		try {
			parsedDate = DateUtils.parseDate(date02, new String[]{"yyyy-MM-dd", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss"});
			System.out.println("yyyy/MM/dd をパースして出力してみる");
			System.out.println(date02);
			System.out.println(parsedDate);
			System.out.println(DateFormatUtils.ISO_DATE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_DATETIME_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_TIME_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(parsedDate));
			System.out.println(DateFormatUtils.SMTP_DATETIME_FORMAT.format(parsedDate) + "\n");
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}

実行結果

現在日時を出力
Mon Apr 11 15:31:37 JST 2011

DateFormatUtils で現在日時を出力する
2011-04-11
2011-04-11+09:00
2011-04-11T15:31:37
2011-04-11T15:31:37+09:00
T15:31:37
15:31:37
15:31:37+09:00
T15:31:37+09:00
Mon, 11 Apr 2011 15:31:37 +0900
少しだけ工夫をしてみる
2011-04-11 15:31:37

yyyy/MM/dd HH:mm:ss をパースして出力してみる
2011/04/01 13:32:09
Fri Apr 01 13:32:09 JST 2011
2011-04-01
2011-04-01+09:00
2011-04-01T13:32:09
2011-04-01T13:32:09+09:00
T13:32:09
13:32:09
13:32:09+09:00
T13:32:09+09:00
Fri, 01 Apr 2011 13:32:09 +0900

yyyy/MM/dd をパースして出力してみる
2011-04-02
Sat Apr 02 00:00:00 JST 2011
2011-04-02
2011-04-02+09:00
2011-04-02T00:00:00
2011-04-02T00:00:00+09:00
T00:00:00
00:00:00
00:00:00+09:00
T00:00:00+09:00
Sat, 02 Apr 2011 00:00:00 +0900