Java - System.currentTimeMillis() 是否和时区有关?

Java的System.currentTimeMillis()返回的是从1970年1月1日格林威治时间0点到当前时间的毫秒数。该值会受到服务器时区的影响,不同时区的服务器运行结果会有相应时区的偏移。例如,在UTC时区的服务器上,结果为GMT+0,而在GMT+8时区的服务器上,结果则为GMT+8。因此,使用该方法时需要注意时区对时间计算的影响,尤其是在生成JWT签名和判断过期时间时。
摘要由CSDN通过智能技术生成

首先我们看一下代码

    public static void timeZonePrint() {
        LocalDateTime now = LocalDateTime.now();

        // 获取秒数 gmt+8
        Long second8 = now.toEpochSecond(ZoneOffset.of("+8"));
        // 获取毫秒数 gmt+8
        Long milliSecond8 = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();

        // 获取秒数 gmt+0
        Long second0 = now.toEpochSecond(ZoneOffset.of("+0"));
        // 获取毫秒数 gmt+0
        Long milliSecond0 = now.toInstant(ZoneOffset.of("+0")).toEpochMilli();

        // 获取秒数 currentTimeMillis
        long totalMilliSeconds = System.currentTimeMillis();

        TimeZone timeZone = TimeZone.getDefault();
        System.out.println("System Default TimeZone: " + timeZone.getID());

        System.out.println("now:            " + now);
        System.out.println("+8 second:      " + second8);
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值