Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android How to check hour time is between two times?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
            }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading