I'm using kotlinx.Clock.now() to get current date. Next I need to convert this date to LocalDateTime, here is my code:
Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
It works well. However, I need to get today's day, but exactly the beginning of today's day, that is 00:00:00. Is it possible to reset the time to 00:00:00?
First, get today's date as a
LocalDate.You can do it the long way:
Or you can simplify using
Clock.todayIn:With either of the above, you can now get the starting
Instantof the day usingLocalDate.atStartOfDayIn.From there if you need a
LocalDateTime, you can get that as well:Putting it all together:
I recommend
atStartOfDayIn, as it will correctly return the starting point of the day - which is not necessarily00:00.For example, on March 12th 2023 in Cuba (tz:
America/Havana), the day started at01:00, because it was the first day of daylight saving time, and in that particular time zone, the transition occurs at00:00. (In other words, the local time goes from23:59to01:00on that day.). There are many other cases like this.