I have a situation. I have open and close time in string format from json response. How to check current hour is between open and close hour?
Assuming openHour is "09:00:00:" and closeHour is "21:00:00".
I am new to android and do not know ho start this logic.
>Solution :
First of all, get the current calendar instance. Parse open and close hour strings and set the hour to the calendar. Then compare both open and closed calenders with the current calendar.
val date = Calendar.getInstance()
val openCalender = Calendar.getInstance()
val closeCalendar = Calendar.getInstance()
val dayToday = android.text.format.DateFormat.format("EEEE", date).toString()
val todayObj = chatOpenHours?.find { it.dayOfWeek == dayToday }
Log.e(TAG, "handleHeroChatAnimation: "+chatOpenHours.toString(), )
if (todayObj != null) {
val openUnits = todayObj.openTime?.split(":")
val openHour = openUnits?.get(0)
val openMinutes = openUnits?.get(1)
val openSeconds = openUnits?.get(2)
openCalender.set(Calendar.HOUR_OF_DAY, openHour?.toInt() ?: 0)
openCalender.set(Calendar.MINUTE, openMinutes?.toInt() ?: 0)
openCalender.set(Calendar.SECOND, openSeconds?.toInt() ?: 0)
val closeUnits = todayObj.closeTime?.split(":")
val closeHour = closeUnits?.get(0)
val closeMinutes = closeUnits?.get(1)
val closeSeconds = closeUnits?.get(2)
closeCalendar.set(Calendar.HOUR_OF_DAY, closeHour?.toInt() ?: 0)
closeCalendar.set(Calendar.MINUTE, closeMinutes?.toInt() ?: 0)
closeCalendar.set(Calendar.SECOND, closeSeconds?.toInt() ?: 0)
val isChatOpen = if (todayObj.openTime == null) {
todayObj.closeTime == null || date <= closeCalendar;
} else if (todayObj.closeTime == null) {
openCalender <= date;
} else {
openCalender.compareTo(date) * date.compareTo(closeCalendar) >= 0;
}