1. <tfoot id='VtOKE'></tfoot>
        <bdo id='VtOKE'></bdo><ul id='VtOKE'></ul>
    1. <i id='VtOKE'><tr id='VtOKE'><dt id='VtOKE'><q id='VtOKE'><span id='VtOKE'><b id='VtOKE'><form id='VtOKE'><ins id='VtOKE'></ins><ul id='VtOKE'></ul><sub id='VtOKE'></sub></form><legend id='VtOKE'></legend><bdo id='VtOKE'><pre id='VtOKE'><center id='VtOKE'></center></pre></bdo></b><th id='VtOKE'></th></span></q></dt></tr></i><div id='VtOKE'><tfoot id='VtOKE'></tfoot><dl id='VtOKE'><fieldset id='VtOKE'></fieldset></dl></div>

      <small id='VtOKE'></small><noframes id='VtOKE'>

    2. <legend id='VtOKE'><style id='VtOKE'><dir id='VtOKE'><q id='VtOKE'></q></dir></style></legend>
    3. JSR 310 :: System.currentTimeMillis() 与 Instant.toEpochMilli

      时间:2023-09-25

          <tfoot id='cQnaY'></tfoot>

          • <small id='cQnaY'></small><noframes id='cQnaY'>

              <tbody id='cQnaY'></tbody>

          • <i id='cQnaY'><tr id='cQnaY'><dt id='cQnaY'><q id='cQnaY'><span id='cQnaY'><b id='cQnaY'><form id='cQnaY'><ins id='cQnaY'></ins><ul id='cQnaY'></ul><sub id='cQnaY'></sub></form><legend id='cQnaY'></legend><bdo id='cQnaY'><pre id='cQnaY'><center id='cQnaY'></center></pre></bdo></b><th id='cQnaY'></th></span></q></dt></tr></i><div id='cQnaY'><tfoot id='cQnaY'></tfoot><dl id='cQnaY'><fieldset id='cQnaY'></fieldset></dl></div>
            • <bdo id='cQnaY'></bdo><ul id='cQnaY'></ul>
              1. <legend id='cQnaY'><style id='cQnaY'><dir id='cQnaY'><q id='cQnaY'></q></dir></style></legend>

                本文介绍了JSR 310 :: System.currentTimeMillis() 与 Instant.toEpochMilli() :: TimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                您能否说明如何为默认系统时区和给定时区获取正确的纪元时间(以毫秒为单位).

                Could you please shed some light on how to obtain correct epoch time in milliseconds for a default system timezone and given timezone.

                1.时区:GMT+3

                2.以下代码片段:

                import java.time.*;
                
                public class Main {        
                    public static void main(String[] args) {
                        System.out.println(LocalDateTime
                            .now()
                            .atZone(ZoneOffset.UTC)
                            .toInstant()
                            .toEpochMilli()
                        );
                        System.out.println(LocalDateTime
                            .now()
                            .atZone(ZoneOffset.of("+3"))
                            .toInstant()
                            .toEpochMilli()
                        );
                        System.out.println(System.currentTimeMillis());
                    }
                }
                

                3.输出:

                1444158955508
                1444148155508
                1444148155508
                

                4.System.currentTimeMillis() 的 JavaDoc 表示返回值将是当前时间与 UTC 1970 年 1 月 1 日午夜之间的差异,以毫秒为单位.

                1. GMT+3LocalDateTime 的输出与 System.currentTimeMillis() 的输出相同,尽管 LocalDateTime 的文档code>System.currentTimeMillis() 提到 UTC?
                2. UTCLocalDateTime 输出不同于 System.currentTimeMillis(),尽管 System.currentTimeMillis 的文档() 提到 UTC?
                1. the output of the LocalDateTime at GMT+3 is the same as of System.currentTimeMillis(), although the docs for the System.currentTimeMillis() mention UTC?
                2. the output of the LocalDateTime at UTC differs from System.currentTimeMillis(), although the docs for the System.currentTimeMillis() mention UTC?

                推荐答案

                System.currentTimeMillis()Instant.toEpochMilli() 都返回自Unix 时代.尽管 Unix 纪元通常表示为UTC 时间 1970 年 1 月 1 日午夜",但这并不在"任何特定时区.但是瞬间只是时间的瞬间,无论您在哪个时区都是一样的 - 但它会反映不同的本地时间.

                Both System.currentTimeMillis() and Instant.toEpochMilli() return the number of milliseconds since the Unix epoch. That isn't "in" any particular time zone, although the Unix epoch is normally expressed as "midnight on January 1st 1970, UTC". But an instant is just an instant in time, and is the same whichever time zone you're in - but it will reflect a different local time.

                LocalDateTime.atZone(UTC) 的输出不同,因为您说的是获取本地日期和时间,并将其转换为即时就好像它在 UTC 时间zone" - 即使您在创建 LocalDateTime 时隐含地在 UTC+3 时区这样做......这就是它错误"的原因.

                The output of LocalDateTime.atZone(UTC) differs because you're saying "Take the local date and time, and convert it to an instant as if it were in the UTC time zone" - even though when you created that LocalDateTime you did so implicitly in the UTC+3 time zone... that's why it's "wrong".

                LocalDateTime.now() 采用系统默认时区中的本地日期和时间.因此,如果您的时区是 UTC+3,则当前时间是 2015-10-06T16:57:00Z,那么 LocalDateTime.now() 将返回 .2015-10-06T19:57:00.我们称之为 localNow...

                LocalDateTime.now() takes the local date and time in the system default time zone. So if your time zone is UTC+3, the current instant in time is 2015-10-06T16:57:00Z, then LocalDateTime.now() will return .2015-10-06T19:57:00. Let's call that localNow...

                所以 localNow.atZone(ZoneOffset.of("+3")) 将返回一个 ZonedDateTime 代表 2015-10-06T19:57:00+03 - in换句话说,相同的本地日期/时间,但知道"它比 UTC 早 3 小时......所以 toInstant() 将返回一个代表 2015-10 的 Instant-06T16:57:00Z.太好了 - 我们还有当前的日期/时间.

                So localNow.atZone(ZoneOffset.of("+3")) will return a ZonedDateTime representing 2015-10-06T19:57:00+03 - in other words, the same local date/time, but "knowing" that it's 3 hours ahead of UTC... so toInstant() will return an Instant representing 2015-10-06T16:57:00Z. Great - we still have the current date/time.

                localNow.atZone(ZoneOffset.UTC) 将返回代表 2015-10-06T19:57:00Z 的 ZonedDateTime - 换句话说,相同的本地日期/时间,但认为"它已经在 UTC...所以 toInstant() 将返回一个代表 2015-10-06T19:57:00Z 的 Instant .. 这是根本不是当前时间(三小时后).

                But localNow.atZone(ZoneOffset.UTC) will return a ZonedDateTime representing 2015-10-06T19:57:00Z - in other words, the same local date/time, but "thinking" that it's already in UTC... so toInstant() will return an Instant representing 2015-10-06T19:57:00Z.. which isn't the current time at all (it's in three hours).

                这篇关于JSR 310 :: System.currentTimeMillis() 与 Instant.toEpochMilli() :: TimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Java 没有关于所有 IANA 时区的信息 下一篇:使用 Java 查找基元数组中的最大值/最小值

                相关文章

                <small id='6qCYP'></small><noframes id='6qCYP'>

              2. <tfoot id='6qCYP'></tfoot>
                  <bdo id='6qCYP'></bdo><ul id='6qCYP'></ul>

                1. <legend id='6qCYP'><style id='6qCYP'><dir id='6qCYP'><q id='6qCYP'></q></dir></style></legend>
                  <i id='6qCYP'><tr id='6qCYP'><dt id='6qCYP'><q id='6qCYP'><span id='6qCYP'><b id='6qCYP'><form id='6qCYP'><ins id='6qCYP'></ins><ul id='6qCYP'></ul><sub id='6qCYP'></sub></form><legend id='6qCYP'></legend><bdo id='6qCYP'><pre id='6qCYP'><center id='6qCYP'></center></pre></bdo></b><th id='6qCYP'></th></span></q></dt></tr></i><div id='6qCYP'><tfoot id='6qCYP'></tfoot><dl id='6qCYP'><fieldset id='6qCYP'></fieldset></dl></div>