【问题标题】:How to check if time in miliseconds is of today's time and it is under 10 seconds from the current time?如何检查以毫秒为单位的时间是否是今天的时间并且距离当前时间不到 10 秒?
【发布时间】:2021-07-06 11:11:43
【问题描述】:

我从服务器响应中获取时间戳,格式为..

KS100V1C1-2C3AE8176DC1\1 {"timestamp":"3:7:2021 16:01:38","ChannelId_1":100}
KS100V1C1-2C3AE8176DC1\1 {"timestamp":"3:7:2021 16:01:48","ChannelId_1":100}
KS100V1C1-2C3AE8176DC1\1 {"timestamp":"3:7:2021 16:01:58","ChannelId_1":100}

我在 10 秒的间隙内得到这个,如图所示是响应 38sec,48sec,58sec...

我想检查时间戳是否是今天的时间,是否是当前时间 10 秒内的时间。就像时间戳是 "3:7:2021 16:01:38" 而当前时间是 "3:7:2021 16:01:48" 它应该返回 true。

我已将String 转换为Date,然后转换为Long,如下所示:

fun convertTimeToLong(time: String) : Long {
    val formatter: DateFormat = SimpleDateFormat("dd:mm:yyyy hh:mm:ss")
    val date = formatter.parse(time) as Date
    Log.d("LongTime : ", date.time.toString())
    return date.time
}

为了检查时间是否低于 10 秒,我尝试了这个:

private val TEN_SECONDS = 10 * 60 * 10000

fun isTimeUnder10Seconds(timeStamp: Long): Boolean {
    val tenAgo: Long = System.currentTimeMillis() - TEN_SECONDS
    if (timeStamp < tenAgo) {
        Log.d("10Seconds ?"," is older than 10 seconds")
        return true
    } else {
        Log.d("10Seconds ?"," is not older than 10 seconds")
        return false
    }
}

但这似乎没有按预期工作。 请帮忙。 谢谢。。

【问题讨论】:

标签: kotlin timestamp compare current-time


【解决方案1】:

我会通过java.time:

这是一个比较您的示例值的示例(不涉及当前时刻,即在底部):

import java.time.LocalDateTime
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter

fun main() {
    val isValid = isOfTodayAndNotOlderThanTenSeconds("6:7:2021 16:01:38", "6:7:2021 16:01:48")
    println(isValid)
}

fun isOfTodayAndNotOlderThanTenSeconds(time: String, otherTime: String) : Boolean {
    // provide a formatter that parses the timestamp format
    val dtf = DateTimeFormatter.ofPattern("d:M:uuuu HH:mm:ss")
    // provide a time zone
    val zone = ZoneId.of("UTC")
    // parse the two arguments and apply the same zone to each
    val other = LocalDateTime.parse(otherTime, dtf).atZone(zone)
    val thatTime = LocalDateTime.parse(time, dtf).atZone(zone)
    // finally return if the days/dates are equal
    return thatTime.toLocalDate().equals(other.toLocalDate())
            // and the first argument is at most 10 seconds older
            && !thatTime.isBefore(other.minusSeconds(10))
}

这实际上返回/打印true。

如果您想将它与现在的时刻进行比较,请调整此fun 以仅接受一个参数并更改要比较的对象:

fun isOfTodayAndNotOlderThanTenSeconds(time: String) : Boolean {
    // provide a formatter that parses the timestamp format
    val dtf = DateTimeFormatter.ofPattern("d:M:uuuu HH:mm:ss")
    // provide a time zone
    val zone = ZoneId.of("UTC")
    // take the current moment in time in the defined zone
    val other = ZonedDateTime.now(zone)
    // parse the argument and apply the same zone
    val thatTime = LocalDateTime.parse(time, dtf).atZone(zone)
    // finally return if the days/dates are equal
    return thatTime.toLocalDate().equals(other.toLocalDate())
            // and the argument is at most 10 seconds older
            && !thatTime.isBefore(other.minusSeconds(10))
}

【讨论】:

  • 它要求安卓设备版本大于O?不能在较低版本中工作?
  • @Sid 您可以使用 Android API Desugaring 或 ThreeTenABP 以使其在较低版本中工作 (
  • 它给了我一个异常:java.time.format.DateTimeParseException: Text '3:7:2021 19:00:49' 无法解析:ClockHourOfAmPm 的值无效(有效值 1 - 12 ): 19
  • @Sid 你是用大写的 Hs 表示一天中的时间还是用小写?您必须在此模式中使用大写。
猜你喜欢
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 2011-03-09
相关资源
最近更新 更多