在 Kotlin 中将日期和时间转换为 Iso 8601

发布于 2025-01-11 21:42:33 字数 277 浏览 5 评论 0原文

我的应用程序中有一个选择器,可以选择年、月、日、小时和分钟。 更改每个值后,我有一些如下所示的数字:

2022 -> for year
3 -> for month
20 -> for day
16 -> hour
58 -> minute

How can I conversion those values to Iso 8601 date format?

我需要这样的格式:“2022-03-20T16:58:00.288Z”

I have a picker in my app that I can select the Year, Month, Day, Hour, and minute.
After changing each of them I have some numbers like this:

2022 -> for year
3 -> for month
20 -> for day
16 -> hour
58 -> minute

How can I convert these values to Iso 8601 date format?

I need like this format: "2022-03-20T16:58:00.288Z"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残疾 2025-01-18 21:42:33

切勿使用 CalendarSimpleDateFormatTimeZone 或其他旧版日期时间类。

使用 java.time

val localDate = LocalDateTime.of(2022, 1, 3, 16, 36)
//LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) 
val offsetDate = OffsetDateTime.of(localDate, ZoneOffset.UTC)

ZoneOffset.UTC 的 OffsetDateTime - 表示当前时间是 UTC 时区(LocalDate 没有任何时区信息),可以更改为系统默认值 (OffsetDateTime.now().offset) 或自定义区域偏移 (ZoneOffset.of("+02:00"))

转换为 ISO 格式

val stringDate = offsetDate.format(DateTimeFormatter.ISO_DATE_TIME)
Log.d("stringDate", "Date  $stringDate")

输出: 2022-01-03T11:36:00Z

注意: java.time 类或 Java8 仅适用于 Android 8 和如上所述,要在较低版本中使用这些类,您需要启用脱糖 - 启用脱糖

Never use Calendar, SimpleDateFormat, TimeZone, or the other legacy date-time classes.

Use OffsetDateTime of java.time classes

val localDate = LocalDateTime.of(2022, 1, 3, 16, 36)
//LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) 
val offsetDate = OffsetDateTime.of(localDate, ZoneOffset.UTC)

ZoneOffset.UTC - impleis that current time is of UTC time zone (LocalDate doesn't have any timezone information), can change to system default (OffsetDateTime.now().offset) or custom Zone offset (ZoneOffset.of("+02:00"))

Convert to ISO Format

val stringDate = offsetDate.format(DateTimeFormatter.ISO_DATE_TIME)
Log.d("stringDate", "Date  $stringDate")

Output : 2022-01-03T11:36:00Z

Note: java.time classes or Java8 only works in Android 8 and above, to use these classes for lower version you need to enable desugaring - Enable Desugaring

才能让你更想念 2025-01-18 21:42:33

尝试使用以下代码:

val date = Calendar.getInstance().apply {
    set(Calendar.YEAR, year)
    set(Calendar.MONTH, month)
    set(Calendar.DAY_OF_MONTH, day)
    set(Calendar.HOUR_OF_DAY, hour)
    set(Calendar.MINUTE, minute)
}.time 
val formattedDate = SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").format(date)

Try with this code:

val date = Calendar.getInstance().apply {
    set(Calendar.YEAR, year)
    set(Calendar.MONTH, month)
    set(Calendar.DAY_OF_MONTH, day)
    set(Calendar.HOUR_OF_DAY, hour)
    set(Calendar.MINUTE, minute)
}.time 
val formattedDate = SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").format(date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文