티스토리 뷰

반응형

JAVA 시작, 종료시간 계산 (분으로 환산)

// 시작시간, 종료시간 받아서 시간 계산
	public static int getMin(String str, String end) {
		
		int result = 0;
		try {
			
			String[] str_arr = str.split(":");
			int str_hour = Integer.parseInt(str_arr[0]);
			int str_minute = Integer.parseInt(str_arr[1]);
			
			String[] end_arr = end.split(":");
			int end_hour = Integer.parseInt(end_arr[0]);
			int end_minute = Integer.parseInt(end_arr[1]);
			
			LocalDateTime sdt = LocalDate.now().atTime(str_hour, str_minute, 00);
			
			int plusDays = 0;
			if(end_hour >= 0 && end_hour < 6) {
				// System.out.println("새벽 0~5 다음날로 인정");
				plusDays = 1;
			}			
			LocalDateTime edt = LocalDate.now().plusDays(plusDays).atTime(end_hour, end_minute, 00);
			
			result = (int) ChronoUnit.MINUTES.between(sdt, edt);
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;		
	}	

위 코드는 종료시간이 00:00~05:00 경우 다음날로 계산됨

 

끗@#%$#!!!

 

 

반응형