「Java」TimestampとSimpleDateFormatクラスのサンプル
書式
public Date parse(String source)throws ParseException
指定された文字列の先頭からテキストを解析して日付を生成します。
使用例
package com.arkgame.study;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFmtDemo {
private static String fmt = "yyyy/MM/dd";
private static String strDate = "2021/07/14";
public static void main(String[] args) {
Timestamp ts = kesuan();
System.out.print("Timestampオブジェクト結果:\n" + ts.getTime());
}
//Timestampオブジェクト
public static Timestamp kesuan() {
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
try {
Date dt = sdf.parse(strDate);
return new Timestamp(dt.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
実行結果
Timestampオブジェクト結果:
1626188400000