[英]How to get current date with reset time 00:00 in kotlin
Hey I am using calendar instance to get current date and time.嘿,我正在使用日历实例来获取当前日期和时间。
private fun getCurrentCalendar() { Calendar.getInstance() }
Output:- Wed Jul 20 21:45:52 GMT+01:00 2022
输出:-
Wed Jul 20 21:45:52 GMT+01:00 2022
If I want to reset time I need to use this function如果我想重置时间我需要使用这个功能
fun resetCalendarTime(calendar: Calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
calendar.set(Calendar.MILLISECOND, 0)
}
it will give this它会给这个
Wed Jul 20 00:00:00 GMT+01:00 2022
My question is there any efficient way of doing that or this is fine in android?我的问题是有什么有效的方法可以做到这一点,或者这在android中很好吗?
Caveat: Java syntax shown here, as I have not yet learned Kotlin.警告:此处显示的 Java 语法,因为我还没有学习 Kotlin。
LocalDate.now( zoneId ).atStartOfDay( zoneId )
The terrible java.util.Calendar
class was years ago supplanted by the modern java.time classes defined in JSR 310. Never use either Date
class, Calendar
, SimpleDateFormat
, or any other legacy date-time class found outside the java.time
package.可怕的
java.util.Calendar
类在几年前被 JSR 310 中定义的现代java.time类所取代。永远不要使用Date
类、 Calendar
、 SimpleDateFormat
或在java.time
包之外找到的任何其他遗留日期时间类。
If you are running Kotlin on a JVM , use java.time classes.如果您在JVM上运行 Kotlin,请使用java.time类。
To get the current moment as seen in a particular time zone, use ZonedDateTime
.要获取特定时区的当前时刻,请使用
ZonedDateTime
。
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
If you want to use the JVM's current default time zone:如果要使用 JVM 当前的默认时区:
ZoneId z = ZoneId.systemDefault() ;
Apparently you want the first moment of the day.显然你想要一天的第一刻。 Your Question makes the mistake of assuming that time is always 00:00.
您的问题错误地假设时间始终为 00:00。 Actually, on some dates in some time zones, the first moment occurs at a different time such as 01:00.
实际上,在某些时区的某些日期,第一个时刻发生在不同的时间,例如 01:00。
👉 So let java.time determine the first moment of the day. 👉 所以让java.time决定一天中的第一个时刻。 Call
LocalDate#atStartOfDay
.调用
LocalDate#atStartOfDay
。
A LocalDate
represents a date-only value, without time-of-day, and without time zone or offset. LocalDate
表示仅日期值,没有时间,也没有时区或偏移量。 Calling atStartOfDay
returns a ZonedDateTime
, like that seen above.调用
atStartOfDay
返回ZonedDateTime
,如上所示。
LocalDate today = LocalDate.now( z ) ;
ZonedDateTime startOfToday = today.atStartOfDay( z ) ;
Here is an example of a day starting at 1 AM.这是从凌晨 1 点开始的一天的示例。
ZoneId z = ZoneId.of( "Asia/Amman" ) ;
LocalDate ld = LocalDate.of( 2021 , 3 , 26 ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;
See this code run live at Ideone.com .请参阅在 Ideone.com 上实时运行的代码。 Notice the time is 01:00.
注意时间是 01:00。
2021-03-26T01:00+03:00[Asia/Amman]
2021-03-26T01:00+03:00[亚洲/安曼]
To generate text in standard ISO 8601 format wisely extended to append the name of the time zone in square brackets, merely call ZonedDateTime#toString
.要生成标准ISO 8601格式的文本,明智地扩展以在方括号中附加时区名称,只需调用
ZonedDateTime#toString
。 See example result quoted directly above this paragraph.请参阅本段上方直接引用的示例结果。
To generate text in other formats, use DateTimeFormatter
, and optionally DateTimeFormatterBuilder
.要生成其他格式的文本,请使用
DateTimeFormatter
和可选的DateTimeFormatterBuilder
。 To automatically localize, use DateTimeFormatter#ofLocalizedDateTime
.要自动本地化,请使用
DateTimeFormatter#ofLocalizedDateTime
。
All of this has been covered many many times on Stack Overflow.所有这些都在 Stack Overflow 上多次介绍过。 Search to learn more.
搜索以了解更多信息。
I recall that some part of the Kotlin community was working on a Kotlin-based port of a subset of java.time functionality to be used for Kotlin-based apps not running on a JVM.我记得 Kotlin 社区的某些部分正在开发基于 Kotlin 的java.time功能子集的端口,用于不在 JVM 上运行的基于 Kotlin 的应用程序。
I imagine that ported library works similar to code shown above.我想移植的库的工作方式类似于上面显示的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.