본문 바로가기

안드로이드

자바( JAVA) 현재시각이 특정 시각 범위 안에 드는지 체크(영업시간)

// 현재 시각이 영업시간인지 체크 .. 다음날 넘어갈때도 체크 
checkTime(“08:00”,”03:00”);


public static boolean checkTime(String start_time , String end_time){

		try {

			int openHH = Integer.parseInt(start_time.split(":")[0]);
			int openMM = Integer.parseInt(start_time.split(":")[1]);

			int endHH = Integer.parseInt(end_time.split(":")[0]);
			int endMM = Integer.parseInt(end_time.split(":")[1]);

			LocalDateTime currentDateTime = LocalDateTime.now();

			// Define the start time and end time for the specified period
			LocalTime startTime = LocalTime.of(openHH, openMM); 
			LocalTime endTime = LocalTime.of(endHH, endMM);    

			// If the end time is before the start time, it means the period crosses midnight.
			// Adjust the end time to be on the next day.
			if (endTime.isBefore(startTime)) {
				currentDateTime = currentDateTime.plusDays(1);
			}

			// Set the start time and end time on the current date
			LocalDateTime startDateTime = currentDateTime.with(startTime);
			LocalDateTime endDateTime = currentDateTime.with(endTime);

			// Check if the current date and time fall within the specified period
			if (currentDateTime.isAfter(startDateTime) || currentDateTime.isBefore(endDateTime)) {
				return true;
			} else {
				return false;
			}


		}catch (Exception e){
			return true;
		}

	}