Java

Date 연산

지네딘 주안 2020. 7. 27. 16:42

// String -> SimpleDateFormat("yyyy-MM-dd HH:mm:ss") -> String형식으로 변환

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(esDocument.getTimestamp());
String timestamp = format.format(date);

 

// 연산을 위해 DateTime으로 변환
DateTime to = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime(timestamp);
DateTime from = to.plusMonths(-1);

 

 

 

 

input string이 어떤 형식으로 들어올 지 모르기 때문에 SimpleDateFormate 사용

예)

2020-07-20 00:00:00.363 으로 들어오는 것을 기대하지만

2020-07-20 00:00:00.36 으로 들어오는 값도 있었음