「Java入門」日付文字列をDate型へ変換する
1.String -> Date
String dateStr = “2017/02/04 12:34:23";
Date date = new Date();
DateFormat sdf = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss");
try {
date = sdf.parse(dateStr);
System.out.println(date.toString());
} catch (Exception e) {
e.printStackTrace();
}
2.Date -> String
String dateStr = “";
Date date = new Date();
DateFormat sdf = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss");
DateFormat sdf2 = new SimpleDateFormat(“yyyy-MM-dd HH/mm/ss");
try {
dateStr = sdf.format(date);
System.out.println(dateStr);
dateStr = sdf2.format(date);
System.out.println(dateStr);
} catch (Exception e) {
e.printStackTrace();
}