0
点赞
收藏
分享

微信扫一扫

javaJDK8时间类1之ZoneId时区

一:ZoneId的介绍

ZoneId用于标识用于在Instant和LocalDateTime之间进行转换的规则。ID有两种不同的类型:

  1. •固定的偏移量-相对于UTC /格林威治标准时间的完全解析的偏移量,它对所有本地日期时间都使用相同的偏移量
  2. •地理区域-适用于查找与UTC /格林威治的偏移量的一组特定规则的区域
  3. 大多数固定偏移量由ZoneOffset表示。在任何ZoneId上调用normalized()将确保将固定的偏移量ID表示为ZoneOffset。
  4. ZoneRules定义了描述偏移量何时更改以及如何更改的实际规则。此类仅是用于获取基础规则的ID。之所以采用这种方法,是因为规则是由政府定义且频繁更改的,而该ID是稳定的。

二.举例说明

1.

  • static Set<string> getAvailableZoneIds()        获取Java中支持的所有时区
  • static ZoneId systemDefault()                   获取系统默认时区
  • static Zoneld of(string zoneld)                 获取一个指定时区

案例截图如下:

                         javaJDK8时间类1之ZoneId时区_System

打印结果如下:

                         javaJDK8时间类1之ZoneId时区_获取当前时间_02

2.

  • static Instant now()                      获取当前时间的Instant对象(标准时间)
  • static Instant ofXxxx(long epochMilli)    根据(秒/毫秒/纳秒)获取Instant对象
  • ZonedDateTime atZone(ZoneIdzone)          指定时区
  • boolean isxxx(Instant otherInstant)       判断系列的方法
  • Instant minusXxx(long millisToSubtract)   减少时间系列的方法
  • Instant plusXxx(long millisToSubtract)    增加时间系列的方法

举例截图如下:

                         javaJDK8时间类1之ZoneId时区_偏移量_03

                         javaJDK8时间类1之ZoneId时区_System_04

                         javaJDK8时间类1之ZoneId时区_System_05

打印结果截图如下:

                         javaJDK8时间类1之ZoneId时区_System_06

案例所有代码如下:

 public static void main(String[] args) {
                /*     static Set<string> getAvailableZoneIds()        获取Java中支持的所有时区
                       static ZoneId systemDefault()                   获取系统默认时区
                       static Zoneld of(string zoneld)                 获取一个指定时区

                                                  */

        //1.利用Set集合获取所有的时区
        Set<String> ZoneIds = ZoneId.getAvailableZoneIds();
        //利用.size()查看集合的长度
        System.out.println(ZoneIds.size());
        //打印当前的时区
        System.out.println(ZoneIds);

        //2,利用systemDefault()方法获取当前系统的默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);

        //3.获取指定的时区 Asia/Aqtau
        ZoneId zoneId1 = ZoneId.of("Asia/Aqtau");
        System.out.println(zoneId1);


        System.out.println("-----------------------------------");


                      /*
                   static Instant now()                      获取当前时间的Instant对象(标准时间)
                   static Instant ofXxxx(long epochMilli)    根据(秒/毫秒/纳秒)获取Instant对象
                   ZonedDateTime atZone(ZoneIdzone)          指定时区
                   boolean isxxx(Instant otherInstant)       判断系列的方法
                   Instant minusXxx(long millisToSubtract)   减少时间系列的方法
                   Instant plusXxx(long millisToSubtract)    增加时间系列的方法

                                                  */

        //1.获取当前时间的Instant对象(标准时间)
        Instant now1 = Instant.now();
        //打印结果
        System.out.println(now1);//2023-07-13T14:53:06.836319900Z

        //2.根据(秒、毫秒、纳秒等)来创建Instanr对象
        Instant instant1 = Instant.ofEpochMilli(30000L);
        System.out.println(instant1);//1970-01-01T00:00:30Z

        Instant instant2 = Instant.ofEpochSecond(40000L);
        System.out.println(instant2);//1970-01-01T11:06:40Z


        Instant instant3 = Instant.ofEpochSecond(2000L, 500000000L);
        System.out.println(instant3);//1970-01-01T00:33:20.500Z


        //3.指定时区
        ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Aqtau"));
        System.out.println(time);//2023-07-13T19:53:06.839406600+05:00[Asia/Aqtau]

        //4.isxxx判断
        Instant instant4 = Instant.ofEpochMilli(10L);
        Instant instant5 = Instant.ofEpochMilli(5000L);


        //5,用于时间的判断
        //isBefore: 判断调用者代表的时间是否再参数表示时间的前面
        boolean result1 = instant4.isBefore(instant5);
        System.out.println(result1);//true


        //isAfter: 判断调用者代表的时间是否再参数表示时间的后面
        boolean result2 = instant4.isAfter(instant5);
        System.out.println(result2);//flase

        //6. Instant minusXxx(long millisToSubtract)   减少时间系列的方法
        Instant instant6 = Instant.ofEpochMilli(3000L);
        System.out.println(instant6);//1970-01-01T00:00:03Z

        //减少时间系列的方法
        Instant instant7 = instant6.minusSeconds(1);
        System.out.println(instant7);//1970-01-01T00:00:02Z


        //增加时间系列的方法
        Instant instant8 = instant7.plusSeconds(200);
        System.out.println(instant8);//1970-01-01T00:03:22Z

    }

举报

相关推荐

0 条评论