How to get the currect hour and minutes in Kotlin?


#1

Hello Kotlin Community

I’ve recently tried to use the current time and minutes for a personal project. The issue is with the sintaxis from Programiz(https://www.programiz.com/kotlin-programming/examples/current-date-time), rather getting further than 12:00 it gets back to 1 o’clock.

Example:
What I want: 11:00;12:00;13:00;14:00;
What Kotlin saves: 11:00;12:00;1:00,2:00

Thanks in advance. And sorry for my bad English.

UPDATE: The problem was fixed thanks to user Eliote


#2

Can you please just paste you code here, so that I can run it on my computer and find out what the problem is.
In addition, I think it’s probably something relating with Java but not Kotlin itself.


#3

var currentDateTime=LocalDateTime.now()

var time= currentDateTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)).removeSuffix(" AM").removeSuffix(" PM")

println(time)


#4

Take a look at https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
You are getting the date formatted as you asked.
Try this:

var time = currentDateTime.format(DateTimeFormatter.ofPattern("HH:mm"))

#5

Thanks for the help.