초를 입력 받아서 월, 일, 시간, 분, 초 출력하기
public String timeDiffDays (String occTime, String checkTime) throws Exception {
/* 변수 세팅 */
String timeDiffDay = "";
/* 날짜값 초기화 세팅 방식 */
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/* 두개의 날짜 가져와서 날짜값으로 세팅 */
LocalDateTime startDateTime = LocalDateTime.parse(occTime, formatter);
LocalDateTime endDateTime = LocalDateTime.parse(checkTime, formatter);
/* 두 날짜의 차이 값 년, 월, 주, 일, 시간, 분, 초 */
int Years = (int) ChronoUnit.YEARS.between (startDateTime, endDateTime);
int month = (int) ChronoUnit.MONTHS.between (startDateTime, endDateTime);
//int week = (int) ChronoUnit.WEEKS.between(startDateTime, endDateTime);
int day = (int) ChronoUnit.DAYS.between (startDateTime, endDateTime);
int hour = (int) ChronoUnit.HOURS.between (startDateTime, endDateTime);
//int minutes = (int) ChronoUnit.MINUTES.between(startDateTime, endDateTime);
int seconds = (int) ChronoUnit.SECONDS.between (startDateTime, endDateTime);
/* 각각 계산 */
int monthSet = seconds / (24*60*60*30) - (Years*365);
int daySet = seconds / (24*60*60) - (month*30);
int hourSet = seconds / (60*60) - (day*24);
int minuteSet = seconds / 60 - (hour*60);
int secondSet = seconds % 60;
/* 0의 값이 나오면 안 나오게 timeDiffDay 값에 담아서 return*/
if(monthSet == 0 && daySet == 0 && hourSet == 0 && minuteSet == 0){
timeDiffDay = secondSet + "초";
}else if(monthSet == 0 && daySet == 0 && hourSet == 0){
timeDiffDay = minuteSet + "분 " + secondSet + "초";
}else if(monthSet == 0 && daySet == 0){
timeDiffDay = hourSet + "시간 " + minuteSet + "분 " + secondSet + "초";
}else if(monthSet == 0){
timeDiffDay = daySet + "일 " + hourSet + "시간 " + minuteSet + "분 "+ secondSet + "초";
}else {
timeDiffDay = monthSet + "개월 " + daySet + "일 " + hourSet + "시간 " + minuteSet + "분 "+ secondSet + "초";
}
logger.info(timeDiffDay);
return timeDiffDay;
}
참고문서
https://sso-feeling.tistory.com/40
https://cornswrold.tistory.com/489
https://java119.tistory.com/52
https://search.naver.com/search.naver?where=nexearch&sm=tab_etc&mra=blM0&qvt=0&query=%EC%8B%9C%EA%B0%84%EB%B3%80%ED%99%98