Kotlin:如何获取一天的开始时间和结束时间?

huangapple go评论90阅读模式
英文:

Kotlin: how to get the start time of a day and end time of a day?

问题

将Unix纪元开始的输入表示日期,需要将日期转换为同一日期的一天的开始时间和结束时间,有哪些在Kotlin中实现这个目标的可能选项?

英文:

My input is unix seconds from epoch, which represents the date, that I need to work with, I need to convert the date to start time of a day and end time of a day of the same date, what are possible options to do so in Kotlin?

答案1

得分: 2

你可以使用 LocalDate 轻松地实现这种行为,使用纪元时间

val localDate = Instant.ofEpochMilli(timeInEpoch).atZone(ZoneId.systemDefault()).toLocalDate()
val startDate = localDate.atStartOfDay() // (or) localDate.atTime(LocalTime.MIN)
val endDate = localDate.atTime(LocalTime.MAX)
英文:

You could use LocalDate to easily achieve this behaviour using epoch

val localDate = Instant.ofEpochMilli(timeInEpoch).atZone(ZoneId.systemDefault()).toLocalDate()
val startDate = localDate.atStartOfDay() // (or) localDate.atTime(LocalTime.MIN)
val endDate = localDate.atTime(LocalTime.MAX)

huangapple
  • 本文由 发表于 2023年5月30日 00:33:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定