[Source | Java] String to Date & Date to Timestamp

Java에서 String을 Timestamp로 변환하는 코드를 정리한다.


작성일 : 2021-06-18


// Define
String dateStr = "2021-06-18 08:32:57";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

1> String to Date

Date date = dateFormat.parse(dateStr);
System.out.println(date);
System.out.println(dateFormat.format(date));

2> Date to Timestamp

Long timestamp = date.getTime();
System.out.println(timestamp);

3> Timestamp to String

[Link – Timestamp to Date & Date to String]