从日历 Android Kotlin 获取 UTC 日期时间戳

发布于 2025-01-10 07:01:57 字数 367 浏览 0 评论 0 原文

我正在尝试以 UTC 格式获取 时区日期,并取决于用户选择的出生地和时区将其保存在数据库中,但在设置时区并尝试检索新值后,它返回旧格式,而不是 UTC

val calendar = Calendar.getInstance().apply {
        timeZone = TimeZone.getTimeZone("UTC")
    }
    println("Date: ${calendar.time}")

I/System.out: Date: Sat Feb 26 16:43:08 GMT+02:00 2022

I'm trying to get Date for timezone in UTC format and depend on the user-selected birthplace and timezone save it in the database but after setting the timezone and try to retrieve the new value it's returning the old format, not in UTC

val calendar = Calendar.getInstance().apply {
        timeZone = TimeZone.getTimeZone("UTC")
    }
    println("Date: ${calendar.time}")

I/System.out: Date: Sat Feb 26 16:43:08 GMT+02:00 2022

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

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

发布评论

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

评论(1

我们只是彼此的过ke 2025-01-17 07:01:57

(注:这个答案使用Java语法,因为我没有学过Kotlin。你可以轻松翻译。)

tl;dr

永远不要使用 Calendar, SimpleDateFormat, TimeZone< /code>,或其他旧的日期时间类。

使用带有 java.time 类的格式化程序对象生成文本。

ZonedDateTime.now( 
    ZoneId.of( "Pacific/Auckland" ) 
)
.format(
    DateTimeFormatter
    .ofLocalizedDateTime( FormatStyle.LONG )
    .withLocale( Locale.GERMANY )
)

请参阅在 IdeOne.com 上实时运行的代码

  • 2022 年 2 月 10:30:06 NZDT
  • 从类似于 SQL 标准类型 TIMESTAMP WITH TIME ZONE 类型的数据库列中检索时刻作为 OffsetDateTime 对象。调整到您想要的时区。使用 DateTimeFormatter 生成文本。

    OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
    ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
    String output = zdt.format( myFormatter ) ;
    

    详细信息

    您正在使用可怕的日期时间类,这些类现在已经成为遗留的,多年前已被 JSR 310 中定义的现代 java.time 类取代。

    (Note: This Answer uses Java syntax, as I have not learned Kotlin. You can translate easily.)

    tl;dr

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

    Generate text using a formatter object with the java.time classes.

    ZonedDateTime.now( 
        ZoneId.of( "Pacific/Auckland" ) 
    )
    .format(
        DateTimeFormatter
        .ofLocalizedDateTime( FormatStyle.LONG )
        .withLocale( Locale.GERMANY )
    )
    

    See this code run live at IdeOne.com.

    1. Februar 2022 um 10:30:06 NZDT

    Retrieve a moment from a database column of a type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE as an OffsetDateTime object. Adjust to your desired time zone. Generate text using DateTimeFormatter.

    OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
    ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
    String output = zdt.format( myFormatter ) ;
    

    Details

    You are using terrible date-time classes that are now legacy, years ago supplanted by the modern java.time classes defined in JSR 310.

    ???? Date-time objects do not have a “format”. Date-time objects hold date-time values, not text. We use formatters to parse text into date-time objects, and use formatters to generate text from date-time objects.

    Get current moment as seen in UTC using Instant.

    Instant instant = Instant.now();
    

    For more flexible formatting, convert to an OffsetDateTime.

    OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;
    

    And use OffsetDateTime class when accessing a database column of a type akin to the SQL-standard type TIMESTAMP WITH TIME ZONE.

    OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
    

    To see that same moment as viewed through the wall-clock time used by the people of a particular region, assign a time zone (ZoneId) to produce a ZonedDateTime.

    ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
    ZonedDateTime zdt = instant.atZone( z ) ;
    

    Generate text in a format that is automatically localized.

    Locale locale = Locale.CANADA_FRENCH ;
    DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale ) ;
    String output = zdt.format( f ) ;
    

    See this code run live at IdeOne.com. Notice how the date as well as the time-of-day differs when viewed through the wall-clock time of Japan.

    instant = 2022-02-27T21:23:27.804471Z
    odt = 2022-02-27T21:23:27.804471Z
    zdt = 2022-02-28T06:23:27.804471+09:00[Asia/Tokyo]
    output = lundi 28 février 2022 à 06 h 23 min 27 s heure normale du Japon
    

    All of this has been covered many many times already on Stack Overflow. Search to learn more.


    The java.time classes are built into Java 8 and later. Android 26+ comes bundled with an implementation. For earlier Android, access most of the functionality through the "API desugaring" feature of the latest tooling.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文