3.1_28_3 JDK8新特性【Date】之ChronoUnit 时间单位枚举

相关链接


5. 新API详解


id=14 ChronoUnit 时间单位枚举

  ChronoUnit枚举类比较特殊,需要单独拿出来说一下。

id类名作用
14 Enum ChronoUnit  TemporalAmount 接口的实现类,采用枚举类的方式表示一些 计算常用时间单位。(常用 e.日期加减:作为plus,minus方法的参数,指定计算的最小单位)
  例如: 一纳秒(ChronoUnit.NANOS
      一微秒(ChronoUnit.MICROS
      一毫秒(ChronoUnit.MILLIS
      一秒(ChronoUnit.SECONDS
      一分钟(ChronoUnit.MINUTES
     一小时(ChronoUnit.HOURS
     半天(ChronoUnit.HALF_DAYS
     一天(ChronoUnit.DAYS
     一周(ChronoUnit.WEEKS
     一个月(ChronoUnit.MONTHS
     一年(ChronoUnit.YEARS
     十年(ChronoUnit.DECADES
     百年(一个世纪)(ChronoUnit.CENTURIES
     千年(ChronoUnit.MILLENNIA
     十亿年(一个纪元)(ChronoUnit.ERAS
     2.92277266×1011年(永恒)(ChronoUnit.FOREVER
  实例: 获取举例今天100年以后的本地日期时间
      LocalDate DecadesFromNow = LocalDate.now().plus(1,ChronoUnit.DECADES);
--------------------------------------------------------------------------------------
  TemporalUnit是个接口,一般直接使用该接口在Java8自带的实现类即可(ChronoUnit)
  idea查看uml图方式=> IDEA使用常见问题记录 的2.1.3 uml图
在这里插入图片描述

遍历ChronoUnit枚举中的所有值: => 参考文献

    public static void main(String[] args) {
        ChronoUnit[] values = ChronoUnit.values();
        Arrays.stream(values).forEach(unit -> {
            Duration duration = unit.getDuration();//持续时间
            long seconds = duration.getSeconds();//秒数
            System.out.println("单位(" + unit + ");   " +
                                "持续范围:(" + duration + ");   " +
                                "秒数(" + seconds + " 秒)");
        });
    }

打印结果:

	单位(Nanos);   持续范围:(PT0.000000001S);   秒数(0 秒)
	单位(Micros);   持续范围:(PT0.000001S);   秒数(0 秒)
	单位(Millis);   持续范围:(PT0.001S);   秒数(0 秒)
	单位(Seconds);   持续范围:(PT1S);   秒数(1 秒)
	单位(Minutes);   持续范围:(PT1M);   秒数(60 秒)
	单位(Hours);   持续范围:(PT1H);   秒数(3600 秒)
	单位(HalfDays);   持续范围:(PT12H);   秒数(43200 秒)
	单位(Days);   持续范围:(PT24H);   秒数(86400 秒)
	单位(Weeks);   持续范围:(PT168H);   秒数(604800 秒)
	单位(Months);   持续范围:(PT730H29M6S);   秒数(2629746 秒)
	单位(Years);   持续范围:(PT8765H49M12S);   秒数(31556952 秒)
	单位(Decades);   持续范围:(PT87658H12M);   秒数(315569520 秒)
	单位(Centuries);   持续范围:(PT876582H);   秒数(3155695200 秒)
	单位(Millennia);   持续范围:(PT8765820H);   秒数(31556952000 秒)
	单位(Eras);   持续范围:(PT8765820000000H);   秒数(31556952000000000 秒)
	单位	(Forever);   持续范围:(PT2562047788015215H30M7.999999999S);   秒数(9223372036854775807 秒)

结果说明:

部分翻译原文
① 上面显示的每个 持续范围(Duration) 表示形式上的 PT 前缀表示根据 ISO-8601标准
  持续时间标记(P=Period) 、时间(T=Time)、分钟(M=Mintue)、(S=Second)、小时(H=Hour)
  表示小于1秒的时间单位 (NANOSMICROSMILLIS) 的ChronoUnit的值显示0秒,因为它们小于1秒,且返回值为长整形(Long)。

② ChronoUnit类每个枚举值的含义,在Javadoc(Java注释)中解释的都很到位。 例如,ChronoUnit.ERAS 的Javadoc中指出:“代表时代概念的单位”。 ISO日历系统没有纪元,因此无法在日期或日期时间中添加纪元。 人为的将时代的估计持续时间定义为1,000,000,000年。当与其他日历系统一起使用时,就没有任何限制了。

③ ChronoUnit.FOREVER持续范围(Duration) 为" PT2562047788015215H30M7.999999999S“,对应于2562047788015215小时,30分钟7.999999999秒、或9 223 372 036 854 775 807、或2.92277266×1011年。”
在这里插入图片描述
④ 什么时候会用到 ChronoUnit.FOREVER? 在Javadoc中阐述了其存在的主要原因:“代表永远概念的单位”。 一般与TemporalField一起使用,以表示无限制的字段。例如年份时代。 人为地将时代的估计持续时间被人为地定义为Duration支持的最长持续时间。”
①  The “PT” prefix on each of the Duration‘s string representations shown above indicates that the representation is a “period” duration designation (“P”) and a “time” designation (“T”) per the ISO-8601 standard. The “S”, “M”, and “H” are seconds, minutes, and hours respectively. The values of ChronoUnit that represent time units less than a second (NANOS, MICROS, and MILLIS) show “0 seconds” because they are less than 1 second and the returned value is an integral long.

②  The Javadoc comments on each value defined in the ChronoUnit class are well written. They follow what in my mind is a Javadoc “best practice”: place a concise but informative initial sentence in the Javadoc to show up in the “Method Summary” section of the generated HTML page and place additional useful details in sentences after that initial summary sentence. For example, the Javadoc comment for ChronoUnit.ERAS states, “Unit that represents the concept of an era. The ISO calendar system doesn’t have eras thus it is impossible to add an era to a date or date-time. The estimated duration of the era is artificially defined as 1,000,000,000 Years. When used with other calendar systems there are no restrictions on the unit.” The bolded sentence (I added that emphasis) is what shows up in the “Method Summary” and the entire text shown here is displayed above the method in its complete explanation.

③  One of the more interesting values in the ChronoUnit enum is FOREVER. As the output of the code listing above demonstrated, the FOREVER value has a Duration of “PT2562047788015215H30M7.999999999S”, corresponding to 2562047788015215 hours, 30 minutes, and 7.999999999 seconds. Or, as Grzegorz Gajos expresses it, “Java defines forever as 9 223 372 036 854 775 807 seconds. Which is 2.92277266 × 1011 years. Better be sure to schedule Java upgrade in your application before times run out.”

④  When would ChronoUnit.FOREVER be useful? Its Javadoc-based description explains its primary reason for existence: “Artificial unit that represents the concept of forever. This is primarily used with TemporalField to represent unbounded fields such as the year or era. The estimated duration of the era is artificially defined as the largest duration supported by Duration.”

22/02/11

M

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值