Yeji's Tech Notes
반응형

1. 가정

주어진 시간(LocalDateTime)으로 현재시간 +-5분사이에 존재하는지 판단하는 케이스

 

2. 특이사항

날짜는 다를 수 있다. 시간으로만 판단해야된다..

 

처음엔 단순하게 LocalDateTime.getMinute() < 5, LocalDateTime.getMinute() > 5 으로 판단하였다..

이럴때 생기는 문제는 8시00분일때가 문제가 됐다..

00분일때는 55분 05분 사이일때를 판단해야되는데 이 상황판단이 불가능 했을뿐만아니라 시간도 같이 판단해줘야되기 때문에 불가능한 케이스라고 판단하였다..

 

밀리세컨즈, 아니면 calendar를 사용해서 차이를 구하려고 했지만, 날짜가 다르다는 변수가 존재해 이또한 불가능 했다...

 

그래서,  LocalTime의 .isBefore(), .isAfter()를 사용한 방법을 택했다.

 

public boolean diffTime(LocalDateTime setTime){
	
    LocalDateTime currentTime = LocalDateTime.now();
    
    String currentMin = currentTime.getMinute()<10 ? "0"+String.valueOf(currentTime.getMinute()));
    String currentHour = currentTime.getHour()<10 ? "0"+String.valueOf(currentTime.getHour());
    
    LocalDateTime minusTime = setTime.minusMinutes(5);
    LocalDateTime plusTime = setTime.plusMinutes(5);
    
    LocalTime time = LocalTime.parse(currentHour+":"+currentMin);
    
    boolean betweenTime = isBetween(setTime,time,
                                    LocalTime.of(minusTime.getHour(),minusHour.getMinute()),
                                    LocalTime.of(plusTime.getHour(),plusTime.getMinute());
                  
    return betweenTime;
 }
 
 public static boolean isBetween(LocalDateTime setTime, LocalTime candidate, LocalTime start, LocalTime end){
 	LocalTime validateTime = LocalTime.of(setTime.getHour(), setTime.getMinute());

	if(validateTime.equals(LocalTime.of(00, 00))) {
			
		if(candidate.getHour()==23&&candidate.getMinute()>=55 || candidate.getHour()==0&&candidate.getMinute()<=5) {
				return true;
		}
		return false;
	}
		
	return !candidate.isBefore(start) && !candidate.isAfter(end);
}
	

 

여기 방식에서도 한가지 문제점을 파악했다... 00시 00분일때는 판단이 안되는 것이였다.

그래서 이부분만 예외사항 체크를 해주었다.

 

currentMin, currentHour에서 10보다 작을 경우에 0을 붙여주는데, LocalTime.parse에서는 HH:mm 형식이여야 하기때문에 10:1은 불가능하기때문에 넣어줬다.

반응형
profile

Yeji's Tech Notes

@Jop

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!