把24小时以半小时为刻度 划分成48个时间片段

2022-09-04  本文已影响0人  别看后面有人

24小时分成48个碎片时间并且判断是否时间已经过去

   //24小时分成48个碎片时间
    fun timeRangeList(dateStr:String): ArrayList<TimeRangeBean> {
        val nowTimeDf=SimpleDateFormat("M月dd日")
        val nowTime=Date()
        val nowDate=nowTimeDf.format(nowTime)
        var isNowDate=false
        if (dateStr==nowDate){
            isNowDate=true
        }
        val timeList= ArrayList<TimeRangeBean>()
        val day= fromStrToLongEnd(dateStr)
        val df=SimpleDateFormat("00:00")
        val format=SimpleDateFormat("HH:mm")

        val time=df.format(day)
        var date=df.parse(time)
       for (i in 0 until 48){
           val calendar=Calendar.getInstance()
           calendar.time=date
           calendar.add(Calendar.MINUTE,30)
           date = calendar.time
           val result=format.format(date)
           val nowTime= getNowTimeToLong(getNowTime())
           val allTime= getNowTimeToLong(result)
           var isEnable=false
           isEnable = if (isNowDate){
               nowTime<allTime
           }else{
               true
           }
           val timeSection= TimeRangeBean(result,isEnable)
           timeList.add(timeSection)
       }
        return timeList
    }
  //时间转为long
    fun getNowTimeToLong(nowTime:String):Long{
        val format=SimpleDateFormat("HH:mm")
        return format.parse(nowTime).time
    }

获取今天明天后天和周几的判断

object TimeToStr {
    /**
     * 用于显示时间
     */
    const val TODAY = "今天"
    const val YESTERDAY = "昨天"
    const val TOMORROW = "明天"
    const val BEFORE_YESTERDAY = "前天"
    const val AFTER_TOMORROW = "后天"

    /**
     * 获取对应时间戳转换的今天、明天。。。。的日期
     * @param time
     * @return
     */
    fun getToday(time: String): String {
        val pre = Calendar.getInstance()
        val predate = Date(System.currentTimeMillis())
        pre.time = predate
        val cal = Calendar.getInstance()
        var date: Date? = null
        try {
            date = Date(time.toLong())
        } catch (e: Exception) {
            e.printStackTrace()
        }
        cal.time = date
        if (cal[Calendar.YEAR] == pre[Calendar.YEAR]) {
            val diffDay = (cal[Calendar.DAY_OF_YEAR]
                    - pre[Calendar.DAY_OF_YEAR])
            return showDateDetail(diffDay, time)
        }
        return time
    }

    /**
     * 将日期差显示为今天、明天或者星期
     * @param diffDay
     * @param time
     * @return
     */
    private fun showDateDetail(diffDay: Int, time: String): String {
        return when (diffDay) {
            0 -> TODAY
            1 -> TOMORROW
            2 -> AFTER_TOMORROW
            -1 -> YESTERDAY
            -2 -> BEFORE_YESTERDAY
            else -> getWeek(time)
        }
    }

    /**
     * 计算周几
     */
    fun getWeek(data: String): String {
        val sdr = SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒")
        val lcc = java.lang.Long.valueOf(data)
        val i = data.toLong()
        val times = sdr.format(Date(i))
        var date: Date? = null
        var mydate = 0
        var week = ""
        try {
            date = sdr.parse(times)
            val cd = Calendar.getInstance()
            cd.time = date
            mydate = cd[Calendar.DAY_OF_WEEK]
            // &#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x65E5;&#x671F;&#x8F6C;&#x6362;&#x6210;&#x661F;&#x671F;&#x51E0;
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        if (mydate == 1) {
            week = "周天"
        } else if (mydate == 2) {
            week = "周一"
        } else if (mydate == 3) {
            week = "周二"
        } else if (mydate == 4) {
            week = "周三"
        } else if (mydate == 5) {
            week = "周四"
        } else if (mydate == 6) {
            week = "周五"
        } else if (mydate == 7) {
            week = "周六"
        }
        return week
    }
}
上一篇下一篇

猜你喜欢

热点阅读