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