如何在特定日期的特定时间范围内显示不同的消息

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

是否可以在特定时间范围内的一周中的不同日子里在 TextView 中显示不同的文本?我正在尝试在决策声明中执行此操作,但由于涉及多天和多次,我不确定该使用什么。

  1. 周一至周五,早上 6 点至 9:59am(将文本设置为 "Available")
  2. 周一至周五,上午 10 点至 11:59am(将文本设置为 "Away")
  3. 周五中午 12 点至周一 5:59am(将文本设置为 "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)
        }
    }
    

我建议您使用 ThreeTenABP 库。有了它可以这样写:

    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))

你也可以用java Calendar class 但是不太方便:

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)
}