0

I am getting the timestamp from the server response in a string like..

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}

I am getting this in 10 seconds of gap like shown is response 38sec,48sec,58sec...

I want to check if the timestamp is of today's and is the time under the 10 sec of current time. Like if the timestamp is "3:7:2021 16:01:38" and current time is "3:7:2021 16:01:48" it should return me true.

I have converted the String to Date and then to Long like this :

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
}

and to check if the time is under 10 seconds I tried this :

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

But this is not seemed to be working as expected. Please help. Thank you..

deHaar
  • 17,687
  • 10
  • 38
  • 51
Sid
  • 2,792
  • 9
  • 55
  • 111
  • 1
    I recommend you don’t use `DateFormat` and `SimpleDateFormat`. Those classes are notoriously troublesome and long outdated. Instead use `DateTimeFormatter` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). See the answer. – Ole V.V. Jul 06 '21 at 16:23

1 Answers1

1

I would do that by means of java.time:

Here's an example that compares your example values (and does not involve the current moment in time, that one's at the bottom):

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

This actually returns/prints true.

If you want to compare it with the moment now, adjust this fun to take only one argument and change the object to compare to:

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))
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • it requires android device version greater than O ? wont work in lower versions? – Sid Jul 06 '21 at 12:57
  • 1
    @Sid you can use [Android API Desugaring](https://developer.android.com/studio/write/java8-support) or [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) in order to make it work in lower versions (<26). – deHaar Jul 06 '21 at 13:00
  • its giving me an exception : java.time.format.DateTimeParseException: Text '3:7:2021 19:00:49' could not be parsed: Invalid value for ClockHourOfAmPm (valid values 1 - 12): 19 – Sid Jul 06 '21 at 16:22
  • @Sid Have you taken upper case Hs for the hours of day or lower case? You have to use upper case in this pattern. – deHaar Jul 06 '21 at 16:36