Java SimpleDateFormatクラスを使ってTimestampをStringに変換するサンプル

環境
JavaSE 1.8
Eclispe 4.14.0

構文
1.System.currentTimeMillis()
現在時間を取得します。ミリ秒の時間値を指定します。
2.public SimpleDateFormat(String pattern)
指定されたパターンとデフォルトのFORMATロケールのデフォルト日付フォーマット記号を使ってSimpleDateFormatを構築します。
パラメータ:
pattern – 日付と時刻のフォーマットを記述するパターン

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.test;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class ArkTest {
public static void main(String[] args) throws ParseException {
// Timestampのインスタンスを生成
Timestamp cftamp = new Timestamp(System.currentTimeMillis());
// 現在時間
System.out.println("現在時間: " + cftamp);
// 日付の形式を指定
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
// Timestampを指定形式に変換する
String res = sdf.format(cftamp);
System.out.println("TimestampをStringに変換する" + res);
}
}
package com.arkgame.test; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; public class ArkTest { public static void main(String[] args) throws ParseException { // Timestampのインスタンスを生成 Timestamp cftamp = new Timestamp(System.currentTimeMillis()); // 現在時間 System.out.println("現在時間: " + cftamp); // 日付の形式を指定 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); // Timestampを指定形式に変換する String res = sdf.format(cftamp); System.out.println("TimestampをStringに変換する" + res); } }
package com.arkgame.test;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ArkTest {

      public static void main(String[] args) throws ParseException {
            // Timestampのインスタンスを生成
            Timestamp cftamp = new Timestamp(System.currentTimeMillis());
            // 現在時間
            System.out.println("現在時間: " + cftamp);

            // 日付の形式を指定
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            // Timestampを指定形式に変換する
            String res = sdf.format(cftamp);
            System.out.println("TimestampをStringに変換する" + res);

      }

}

実行結果
現在時間: 2022-11-15 13:36:50.183
TimestampをStringに変換する2022年11月15日

Java

Posted by arkgame