JSR 310 :: System.currentTimeMillis()与Instant.toEpochMilli():: TimeZone [英] JSR 310 :: System.currentTimeMillis() vs Instant.toEpochMilli() :: TimeZone

查看:274
本文介绍了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

1. TimeZone: 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 表示返回的值是当前时间与1970年1月1日午夜之间的差值(以毫秒为单位).

4. JavaDoc for System.currentTimeMillis() that tells that returned value will be the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

  1. GMT+3LocalDateTime的输出与System.currentTimeMillis()相同,尽管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时区一样",即使您创建的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"))将返回代表2015-10-06T19:57:00 + 03的ZonedDateTime-换句话说,相同的本地日期/时间,但知道"它比UTC早3小时. .因此toInstant()将返回表示2015-10-06T16:57:00Z的Instant.太好了-我们仍然拥有当前的日期/时间.

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中.所以将会返回代表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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆