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

时间:2019-07-30 20:55:01

标签: android kotlin calendar android-calendar

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

  1. 星期一至星期五,上午6点至上午9:59(将文本设置为“可用”)
  2. 星期一至星期五,上午10点至上午11:59(将文本设置为“离开”)
  3. 星期五中午12点至星期一5:59(将文本设置为“不可用”)

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

1 个答案:

答案 0 :(得分:2)

我建议您使用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类,但不太方便:

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