Skip to content

Instantly share code, notes, and snippets.

@ola0x
Created August 4, 2020 19:34
Show Gist options
  • Save ola0x/3775153656df9b24209241a4905002e3 to your computer and use it in GitHub Desktop.
Save ola0x/3775153656df9b24209241a4905002e3 to your computer and use it in GitHub Desktop.
class Timer {
fun printDifferenceDateForHours() {
val currentTime = Calendar.getInstance().time
val endDateDay = "03/02/2020 21:00:00"
val format1 = SimpleDateFormat("dd/MM/yyyy hh:mm:ss",Locale.getDefault())
val endDate = format1.parse(endDateDay)
//milliseconds
var different = endDate.time - currentTime.time
countDownTimer = object : CountDownTimer(different, 1000) {
override fun onTick(millisUntilFinished: Long) {
var diff = millisUntilFinished
val secondsInMilli: Long = 1000
val minutesInMilli = secondsInMilli * 60
val hoursInMilli = minutesInMilli * 60
val daysInMilli = hoursInMilli * 24
val elapsedDays = diff / daysInMilli
diff %= daysInMilli
val elapsedHours = diff / hoursInMilli
diff %= hoursInMilli
val elapsedMinutes = diff / minutesInMilli
diff %= minutesInMilli
val elapsedSeconds = diff / secondsInMilli
txt_timeleft.text = "$elapsedDays days $elapsedHours hs $elapsedMinutes min $elapsedSeconds sec"
}
override fun onFinish() {
txt_timeleft.text = "done!"
}
}.start()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment