reza_khalafi
reza_khalafi

Reputation: 6552

How to get hour form Date in Android Kotlin?

I try to get hour from Date data type in Kotlin but get this error:

'getter for hours: Int' is deprecated. Deprecated in Java

And my code:

val date:Date = ...
val hour = date.hours

this is deprecated and getHours() not found ...

Upvotes: 10

Views: 9761

Answers (1)

Nikunj Paradva
Nikunj Paradva

Reputation: 16087

getHours() is deprecated. It replaced by Calendar.get(Calendar.HOUR_OF_DAY)

val date:Date = ... // your date
val cal = Calendar.getInstance()
cal.time = date
val hours = cal.get(Calendar.HOUR_OF_DAY)

Upvotes: 19

Related Questions