LoginSignup
0
1

More than 3 years have passed since last update.

Java の SimpleDateFormat で日時のパース・フォーマットをするサンプルコード

Posted at

概要

  • Java の SimpleDateFormat クラスで日時文字列をパースして Date オブジェクトを文字列化する

サンプルコード

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

/**
 * SimpleDateFormat でパース・フォーマットするサンプル。
 */
public class Sample {

  public static void main(String[] args) {

    String[] sourceList = {
      "1-2-3 4:5:6",
      "99-2-3 4:5:6",
      "999-2-3 4:5:6",
      "2000-12-31 12:34:56",
      "9999-12-31 12:34:56.0001",
      "9999-12-31 12:34:56.000001",
      "10000-12-31 12:34:56",
      "XXXX-YY-ZZ AA:BB:CC",
      "9999X-12X-31X 12X:34X:56X",
      "X9999-X12-X31 X12:X34:X56",
    };

    parseAndFormat(sourceList, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss");
    parseAndFormat(sourceList, "y-M-d H:m:s", "yyyy-MM-dd HH:mm:ss");
    parseAndFormat(sourceList, "yyyy-MM-dd HH:mm:ss", "y-M-d H:m:s");
  }

  /**
   * パースしてフォーマットする。
   * @param sourceList パース対象の文字列の配列
   * @param parsePattern 日時文字列をパースするパターン文字列
   * @param formatPattern Date オブジェクトをフォーマットするパターン文字列
   */
  private static void parseAndFormat(String[] sourceList, String parsePattern, String formatPattern) {

    // パーサーを準備
    SimpleDateFormat parser = new SimpleDateFormat(parsePattern);

    // フォーマッターを準備
    SimpleDateFormat formatter = new SimpleDateFormat(formatPattern);

    System.out.println("****************************************");
    System.out.println("Parse:  " + parsePattern);
    System.out.println("Format: " + formatPattern);

    for (String source : sourceList) {
      try {
        // 日時文字列を Date オブジェクトに変換
        Date date = parser.parse(source);
        // Date オブジェクトを日時文字列に変換
        String text = formatter.format(date);
        System.out.println(source + " -> " + text);
      } catch (ParseException e) {
        System.out.println(source + " -> " + e.toString());
      }
    }

    System.out.println();
  }
}

実行結果

Java 8 (AdoptOpenJDK 1.8.0_272-b10) + macOS Catalina にて実行した例。

****************************************
Parse:  yyyy-MM-dd HH:mm:ss
Format: yyyy-MM-dd HH:mm:ss
1-2-3 4:5:6 -> 0001-02-03 04:05:06
99-2-3 4:5:6 -> 0099-02-03 04:05:06
999-2-3 4:5:6 -> 0999-02-03 04:05:06
2000-12-31 12:34:56 -> 2000-12-31 12:34:56
9999-12-31 12:34:56.0001 -> 9999-12-31 12:34:56
9999-12-31 12:34:56.000001 -> 9999-12-31 12:34:56
10000-12-31 12:34:56 -> 10000-12-31 12:34:56
XXXX-YY-ZZ AA:BB:CC -> java.text.ParseException: Unparseable date: "XXXX-YY-ZZ AA:BB:CC"
9999X-12X-31X 12X:34X:56X -> java.text.ParseException: Unparseable date: "9999X-12X-31X 12X:34X:56X"
X9999-X12-X31 X12:X34:X56 -> java.text.ParseException: Unparseable date: "X9999-X12-X31 X12:X34:X56"

****************************************
Parse:  y-M-d H:m:s
Format: yyyy-MM-dd HH:mm:ss
1-2-3 4:5:6 -> 0001-02-03 04:05:06
99-2-3 4:5:6 -> 1999-02-03 04:05:06
999-2-3 4:5:6 -> 0999-02-03 04:05:06
2000-12-31 12:34:56 -> 2000-12-31 12:34:56
9999-12-31 12:34:56.0001 -> 9999-12-31 12:34:56
9999-12-31 12:34:56.000001 -> 9999-12-31 12:34:56
10000-12-31 12:34:56 -> 10000-12-31 12:34:56
XXXX-YY-ZZ AA:BB:CC -> java.text.ParseException: Unparseable date: "XXXX-YY-ZZ AA:BB:CC"
9999X-12X-31X 12X:34X:56X -> java.text.ParseException: Unparseable date: "9999X-12X-31X 12X:34X:56X"
X9999-X12-X31 X12:X34:X56 -> java.text.ParseException: Unparseable date: "X9999-X12-X31 X12:X34:X56"

****************************************
Parse:  yyyy-MM-dd HH:mm:ss
Format: y-M-d H:m:s
1-2-3 4:5:6 -> 1-2-3 4:5:6
99-2-3 4:5:6 -> 99-2-3 4:5:6
999-2-3 4:5:6 -> 999-2-3 4:5:6
2000-12-31 12:34:56 -> 2000-12-31 12:34:56
9999-12-31 12:34:56.0001 -> 9999-12-31 12:34:56
9999-12-31 12:34:56.000001 -> 9999-12-31 12:34:56
10000-12-31 12:34:56 -> 10000-12-31 12:34:56
XXXX-YY-ZZ AA:BB:CC -> java.text.ParseException: Unparseable date: "XXXX-YY-ZZ AA:BB:CC"
9999X-12X-31X 12X:34X:56X -> java.text.ParseException: Unparseable date: "9999X-12X-31X 12X:34X:56X"
X9999-X12-X31 X12:X34:X56 -> java.text.ParseException: Unparseable date: "X9999-X12-X31 X12:X34:X56"

参考資料

0
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1