kotlin get time milis hour minutes
import java.util.Calendar
fun main() {
val currentTimeInMillis = System.currentTimeMillis()
val calendar = Calendar.getInstance()
calendar.timeInMillis = currentTimeInMillis
val hour = calendar.get(Calendar.HOUR_OF_DAY)
val minute = calendar.get(Calendar.MINUTE)
println("Current time: $hour:$minute")
}
Explanation:
- Import the
Calendar
class fromjava.util
package. - Define the
main()
function. - Get the current time in milliseconds using
System.currentTimeMillis()
and store it incurrentTimeInMillis
. - Create an instance of the
Calendar
class usingCalendar.getInstance()
. - Set the time of the calendar instance to the current time in milliseconds using
calendar.timeInMillis = currentTimeInMillis
. - Retrieve the current hour of the day using
calendar.get(Calendar.HOUR_OF_DAY)
and store it in the variablehour
. - Retrieve the current minute using
calendar.get(Calendar.MINUTE)
and store it in the variableminute
. - Print the current time in the format
hour:minute
usingprintln("Current time: $hour:$minute")
. ```