简体   繁体   English

如何在kotlin中获取重置时间为00:00的当前日期

[英]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。

tl;dr tl;博士

LocalDate.now( zoneId ).atStartOfDay( zoneId )

Details细节

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类、 CalendarSimpleDateFormat或在java.time包之外找到的任何其他遗留日期时间类。

On JVM在 JVM 上

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.搜索以了解更多信息。

Off JVM关闭 JVM

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.

相关问题 Android以毫秒为单位获取当前日期(时间为0:00:000)? - Android Get Current Date (with time being 0:00:000) in Milliseconds? 如何获取我的设备蓝牙 ID。我尝试获取但它们总是返回 02:00:00:00:00:00 - How to get my device bluetooth id.i try to get but they all time return 02:00:00:00:00:00 如何以这种格式获取 TimePicker 的时间 -> “08:00:00”? - How can i get the time of a TimePicker in this format -> "08:00:00"? Android:如何以以下格式“ 2015-10-08T08:09:22.067 + 00:00”立即获取日期时间字符串? - Android : how to get now date time string in following format “2015-10-08T08:09:22.067+00:00”? 每当我再次运行程序时,如何将计时器从00:00:00开始重置? - How do i reset the timer to start from 00:00:00 each time i run my program over again? 如何在 Kotlin 中获取当前的本地日期和时间 - How to get current local date and time in Kotlin 如何在Android上使用GsonBuilder解析此日期2016-03-29T00:00:00 + 01:00 - how to parse this date 2016-03-29T00:00:00+01:00 with GsonBuilder on Android 这个“日期”的时间格式是什么:“2014-08-20 00:00:00 -0500”? - What is the Time format for this “date”: “2014-08-20 00:00:00 -0500”? 如何获得每天上午 9:00 与 Android 系统时间的比较? - How to get everyday AM 9:00 compare with Android System time? 如何在kotlin中以ISO 8601格式获取当前日期和时间? - How to get current date and time in ISO 8601 format in kotlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM