Search code examples
androidkotlincalendarandroid-calendar

How to show different messages during certain time ranges during certain days


Is it possible to show different text within a TextView during different days of the week during specific time ranges? I'm trying to do this within decision making statements but I'm not sure what to use as multiple days & times are involved.

  1. Monday to Friday, 6am until 9:59am (set text to "Available")
  2. Monday to Friday, 10am until 11:59am (set text to "Away")
  3. Friday 12pm until Monday 5:59am (set text to "Unavailable")

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val current = LocalTime.now()
        val formatter = DateTimeFormatter.ofPattern("HH:mm")
        val currentTime: String = current.format(formatter)
        val currentDay: String = LocalDate.now().dayOfWeek.name
    
        if (currentTime == "06:00" && currentDay == "MONDAY") {
            tv.setText(R.string.available)
        } else if (currentTime == "10:00" && currentDay == "MONDAY") {
            tv.setText(R.string.away)
        } else {
            tv.setText(R.string.unavailable)
        }
    } else {
        val date = Date()
        val formatter = SimpleDateFormat("hh:mm", Locale.getDefault())
        val currentTime: String = formatter.format(date)
        val currentDay: String = LocalDate.now().dayOfWeek.name
    
        if (currentTime == "06:00" && currentDay == "MONDAY") {
            tv.setText(R.string.available)
        } else if (currentTime == "10:00" && currentDay == "MONDAY") {
            tv.setText(R.string.away)
        } else {
            tv.setText(R.string.unavailable)
        }
    }
    

Solution

  • I would like to suggest you use a ThreeTenABP library. With it can be written something like this:

        fun main() {
            val now = LocalDateTime.now()
            when (now.dayOfWeek) {
                DayOfWeek.MONDAY,
                DayOfWeek.TUESDAY,
                DayOfWeek.WEDNESDAY,
                DayOfWeek.THURSDAY -> {
                    when {
                        inRange(now, 6, 10) -> tv.setText(R.string.available)
                        inRange(now, 10, 12) -> tv.setText(R.string.away)
                        else -> tv.setText(R.string.unavailable)
                    }
                }
                DayOfWeek.FRIDAY -> {
                    //todo
                }
                else -> {
                    //todo
                }
            }
        }
    
        private fun inRange(now: LocalDateTime, start: Int, end: Int): Boolean =
            now.isAfter(now.withHour(start)) && now.isBefore(now.withHour(end))
    

    you also can use java Calendar class but it not so convenient:

    import java.util.*
    
    fun main() {
        val now = Calendar.getInstance()
        when (now.get(Calendar.DAY_OF_WEEK)) {
            Calendar.MONDAY,
            Calendar.TUESDAY,
            Calendar.WEDNESDAY,
            Calendar.THURSDAY -> {
                when {
                    inRange(now, 6, 10) -> tv.setText(R.string.available)
                    inRange(now, 10, 12) -> tv.setText(R.string.away)
                    else -> tv.setText(R.string.unavailable)
                }
            }
            Calendar.FRIDAY -> {
                //todo
            }
            else -> {
                //todo
            }
        }
    }
    
    private fun inRange(now: Calendar, startHour: Int, startMinutes: Int, endHour: Int, endMinutes: Int): Boolean {
        val startTime: Calendar = now.clone() as Calendar
        startTime.set(Calendar.HOUR_OF_DAY, startHour)
        startTime.set(Calendar.MINUTE, startMinutes)
        val endTime: Calendar = now.clone() as Calendar
        endTime.set(Calendar.HOUR_OF_DAY, endHour)
        endTime.set(Calendar.MINUTE, endMinutes)
        return now.after(startTime) && now.before(endTime)
    }