I am trying to check if the current time (hours and minute) is after or before a certain time, how can I do it in kotlin
Asked
Active
Viewed 6,187 times
3
-
Are you sure your question is clear? I can't find the two terms of the matching – Luca Murra Feb 06 '20 at 14:21
-
@LucaMurra Let me give you an example I want to check is the current time is after 13:59 so I can do an action – 7mood Feb 06 '20 at 14:24
-
1@7mood The question is still underspecified. What is the data type of the time you want to compare to the current time? Is it a String, a Java Date instance, a JavaScript Date instance or maybe something else? Are you using kotlin on the JVM or somewhere else? – Paul Georg Podlech Feb 06 '20 at 14:39
-
Please, do not add things like `[SOLVED]` into the title, that's not how the system works. Furthermore, do not change the question after you have accepted an answer. – Dan Mašek Feb 06 '20 at 16:32
-
I just made the question more clear – 7mood Feb 06 '20 at 16:36
3 Answers
6
You can use the Calendar
class:
val currentTime = Calendar.getInstance()
val timeToMatch = Calendar.getInstance()
timeToMatch[Calendar.HOUR_OF_DAY] = hourToMatch
timeToMatch[Calendar.MINUTE] = minuteToMatch
when {
currentTime == timeToMatch -> // the times are equals
currentTime < timeToMatch -> // currentTime is before timeToMatch
currentTime > timeToMatch -> // currentTime is after timeToMatch
}

IlyaMuravjov
- 2,352
- 1
- 9
- 27

Luca Murra
- 1,848
- 14
- 24
-
I suggest you to do it the other way around, i.e. extracting the hour of day and minute from the current time (see also the last part of my answer)... otherwise on the same hour/minute you will have differences due to millisecond (and/or second) deviation... – Roland Feb 10 '20 at 15:19
-
1see also https://stackoverflow.com/questions/48688736/are-java-util-date-and-java-util-calendar-deprecated – leonardkraemer Feb 10 '20 at 15:36
-
only worked when used: timeToMatch.set(Calendar.HOUR_OF_DAY, HOUR) – Bilal Awwad Jul 16 '21 at 19:47
2
If you have a LocalDateTime
or similar, you can extract the MINUTE_OF_DAY
(which is basically hours + minutes) to compare the time and ignoring the rest, e.g.:
val now = LocalDateTime.now()
val dateToCompare : LocalDateTime = TODO()
val minutesOfDayNow = now.get(ChronoField.MINUTE_OF_DAY)
val minutesOfDayToCompare = dateToCompare.get(ChronoField.MINUTE_OF_DAY)
when {
minutesOfDayNow == minutesOfDayToCompare -> // same hour and minute of day
minutesOfDayNow > minutesOfDayToCompare -> // hours and minutes now are after the time to compare (only in regards to hours and minutes... not day/month/year or whatever)
minutesOfDayNow < minutesOfDayToCompare -> // hours and minutes now are before the time to compare... same conditions apply
}
If you instead have a Date
you may be interested in transforming it to an instance of a java.time
-type before, e.g.:
fun Date.minutesOfDay() = toInstant().atZone(ZoneId.systemDefault()).get(ChronoField.MINUTE_OF_DAY)
val now = Date()
val dateToCompare : Date = TODO()
if (now.minutesOfDay() > dateToCompare.minutesOfDay()) // ...etc. pp.
Finally if you want to use Calendar
just be sure to compare only the things you are interested in, i.e. the hours and minutes and nothing more, e.g.:
val now = Calendar.getInstance()
val nowInMinutes = now[Calendar.HOUR_OF_DAY] * 60 + now[Calendar.MINUTE]
val dateInMinutesToCompare = hours * 60 + minutes
when {
nowInMinutes == dateInMinutesToCompare -> // equal
nowInMinutes > dateInMinutesToCompare -> // now after given time
else -> // now before given time
}

Roland
- 22,259
- 4
- 57
- 84
1
You must first specify now and then the future time in two variables for comparison. You can then compare them with isBefore
and isAfter
to determine true/false on either.
val timeNow = LocalTime.now()
val timeTo = LocalTime.parse("15:00:00")
timeTo.isBefore(timeNow) // boolean for `before`
timeTo.isAfter(timeNow) // boolean for `after`

Grant
- 5,709
- 2
- 38
- 50

Raymond Barrinuevo
- 167
- 1
- 6