-
Notifications
You must be signed in to change notification settings - Fork 109
set hours of LocalDateTime #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
See Date + time arithmetic for an explanation why this is not possible and how to work around it by using |
@svenjacobs thank you , they have to provide extension funs , on the instant for these use cases |
|
@svenjacobs the issue is not about arithmetic operation (plus, minus), but setting to specific local value (e.g. set hours to 10AM). It cannot be done with |
val nowDateTime = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
val time = LocalTime(hour = 10, minute = 0)
val newDateTime = nowDateTime.date.atTime(time) // nowDateTime.date returns LocalDate |
Yes, this is the way, though one must be wary of manipulating That said, we do consider adding such setter-like functions if we manage to make them both flexible and safe. Could you share your use case? Why do you need such code? |
For Example actually change second and millisecond of an Instant is just INSANE!
|
Why do you need that? What's your high-level goal? |
in my case i just need time slots of 1 hour from current time at start of hours, like from "2023-03-27T11:23:33Z" i need to create 2023-03-27T11:00:00Z i think if there is "plusHours" there should be also "withHours" or something like that |
@xanscale Perhaps a function that truncates date/time to the specified unit would better fit your need? |
@ilya-g I suppose that withSecond, withMinute, etc is more generic |
Closing in favor of #325 |
let's say that we have
val nowLocalDateTime = Clock.System.now()
.toLocalDateTime(TimeZone.currentSystemDefault())
I want to set the hours after getting the current LocalDateTime to 10 am .
in java we used
val pickupCalendar =
Calendar.getInstance().apply {
add(Calendar.DATE, 2)
this[Calendar.HOUR_OF_DAY] = 10
this[Calendar.MINUTE] = 0
}
The text was updated successfully, but these errors were encountered: