zoukankan      html  css  js  c++  java
  • JDK8时间新API-2

    1 获取时间戳不再使用System.currentTimeMillis

    @Test
        public void testClock () {
            Clock clock = Clock.systemUTC();
            System.out.println("Clock : " + clock.millis());
            System.out.println("Clock : " + System.currentTimeMillis());
            // Returns time based on system clock zone
            Clock defaultClock = Clock.systemDefaultZone();
            System.out.println("Clock : " + defaultClock.millis());
    
        }

    这两个方法

     Clock.systemUTC()   Clock.systemDefaultZone() 的区别是什么?

    So although both will return the same instant if you ask them what the current instant is, 
    they will not necessarily return the same LocalDateTime, or LocalDate, or ZonedDateTime.
    For example, suppose my system timezone is Asia/Shanghai, which is at an offset of UTC+8 all year round.
    And it is now 5 am on 2020-07-29 in Shanghai.
    If I do LocalDate.now(Clock.systemUTC()), it will tell me 2020-07-28, because it's still the 28th in the UTC timezone.
    If I do LocalDate.now(Clock.systemDefaultZone()), however, it will tell me 2020-07-29,
    because it is 2020-07-29 in Shanghai (which is the system timezone). Now you should see why the parameterless now of date/time related classes use systemDefaultZone.
    It would be weird if I did LocalTime.now() in Shanghai and saw a time that's 8 hours earlier!

    所以最好使用Clock.systemDefaultZone() 拿到本JVM的时钟

  • 相关阅读:
    Bzoj 3654 图样图森波 题解
    1.27号考试记录
    博弈论入门小结
    「考试总结」2020-11-18 爆零
    「补题」考试题泛做
    CSP2020 游记,总结与题解
    Luogu2827 「NOIP2016」 蚯蚓
    【学习笔记】四毛子算法
    「考试反思」2020-11-04 临行
    「考试反思」2020-10-31 警示
  • 原文地址:https://www.cnblogs.com/juniorMa/p/14714346.html
Copyright © 2011-2022 走看看