본문 바로가기
Programing Language/Java

[JAVA] 문자열을 날짜로, 날짜를 문자열로 변환

by pcm9881 2022. 10. 23.

문자열을 날짜로 (String to Date)

 

1.  String to Date

String dateString = "2022-10-23 23:14:10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsedDate = sdf.parse(dateString);

2. String to LocalDateTime

String dateSting = "2022-10-23 23:14:10"; 
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parsedLocalDateTime = LocalDateTime.parse(dateSting, dtf);

 

날짜를 문자열로 (Date to String)

1.  Date to String

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = sdf.format(date);

2. LocalDateTime to String

LocalDateTime now = new LocalDateTime.now();
String parsedLocalDateTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
728x90

댓글