在jdk1.8以后,对表示日期时间的类型进行了重新分类,这里出现了2个新的类,Duraction 和Period

Duraction表示:时间的区间,用来度量秒和纳秒之间的时间值

Period表示:一段时间的区间,用来度量年月日和几天之间的时间值

那清楚了这个类度量的时间范围,那么操作api那岂不是易如反掌

如果对LocalDate LocalTime LocalDateTime Instant不熟悉的,可以看LocalDate LocalTime LocalDateTime Instant的操作与使用

public static void testDuration() {
LocalTime localTime = LocalTime.now();
LocalTime localTime2 = LocalTime.of(19, 19, 19);
Duration duration = Duration.between(localTime, localTime2);
System.out.println(duration);
System.out.println(duration.isZero()); //Duration区间是否为0
System.out.println(duration.isNegative()); //Duration区间是否为负 System.out.println(duration.getSeconds()); //Duration区间值的秒数
System.out.println(duration.getNano()); //Duration区间值的纳秒数
System.out.println(duration.getUnits()); //Duration的度量单位 System.out.println(duration.toDays()); //Duration区间相差几天
System.out.println(duration.toHours()); //Duration区间差几小时
System.out.println(duration.toMinutes()); //Duration区间相差几分钟
System.out.println(duration.toMillis()); //Duration区间相差几毫秒 //============ duration 时间区间的加减 省略 ============
}
public static void testPeriod() {
LocalDate localDate = LocalDate.now();
LocalDate localDate2 = LocalDate.of(2020, 12, 12);
Period period = Period.between(localDate, localDate2); System.out.println(period); System.out.println(period.isZero()); //区间是否为0
System.out.println(period.isNegative()); //区间是否为为负 System.out.println(period.getYears()); //区间的相差几年
System.out.println(period.getMonths()); //区间的相差几月
System.out.println(period.getDays()); //区间的相差几日 System.out.println(period.toTotalMonths()); //区间相差多少个月
//============ duration 时间区间的加减 省略 ============ }

Duration和Period的区别--通俗易懂的更多相关文章

  1. CPU与GPU区别 通俗易懂

    转:https://blog.csdn.net/xiaolang85/article/details/51500340 有网友在网上提问:“为什么现在更多需要用的是 GPU 而不是 CPU,比如挖矿甚 ...

  2. i386和x86-64区别通俗易懂版本(转)

    x86架构首度出现在1978年推出的Intel 8086中央处理器,它是从Intel 8008处理器中发展而来的,而8008则是发展自Intel 4004的.Intel之后又推出了包括80186.80 ...

  3. JDK8中的新时间API:Duration Period和ChronoUnit介绍

    目录 简介 Duration Period ChronoUnit 简介 在JDK8中,引入了三个非常有用的时间相关的API:Duration,Period和ChronoUnit. 他们都是用来对时间进 ...

  4. C++11 std::unique_lock与std::lock_guard区别及多线程应用实例

    C++多线程编程中通常会对共享的数据进行写保护,以防止多线程在对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为.通常的做法是在修改共享数据成员的时候进行加锁--mutex.在使用锁的时候通 ...

  5. Java中timer的schedule()和schedualAtFixedRate()函数的区别

    本文主要讨论java.util.Timer的schedule(timerTask,delay,period)和scheduleAtFixedRate(timerTask,delay,period)的区 ...

  6. C++11 并发指南三(Lock 详解)

    在 <C++11 并发指南三(std::mutex 详解)>一文中我们主要介绍了 C++11 标准中的互斥量(Mutex),并简单介绍了一下两种锁类型.本节将详细介绍一下 C++11 标准 ...

  7. 计算机程序的思维逻辑 (95) - Java 8的日期和时间API

    ​本节继续探讨Java 8的新特性,主要是介绍Java 8对日期和时间API的增强,关于日期和时间,我们在之前已经介绍过两节了,32节介绍了Java 1.8以前的日期和时间API,主要的类是Date和 ...

  8. 7、Dockerfile详解

    参考: https://www.imooc.com/article/details/id/25229 https://www.cnblogs.com/panwenbin-logs/p/8007348. ...

  9. Java编程的逻辑 (95) - Java 8的日期和时间API

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

随机推荐

  1. driud 异常

    异常如下: 十二月 25, 2017 11:37:14 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetProper ...

  2. Java IO管道流

    import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; publi ...

  3. ZR#1005

    ZR#1005 解法: 题解给了一个建图跑最短路的做法,但好像没有必要,因为 $ m $ 没有用,所以直接上完全背包就行了. CODE: #include<iostream> #inclu ...

  4. Django框架,python2和python3共存的情况下,创建Django项目

    一.python2和python3共存的情况下,直接使用 django-admin startproject Django-project 这个时候系统默认使用的是python2创建,可能由于pyth ...

  5. #C++初学记录(acm试题#预处理)

    C - Lucky 7 in the Pocket BaoBao loves number 7 but hates number 4, so he refers to an integer as a ...

  6. org.postgresql.util.PSQLException:致命:抱歉,已经有太多客户了(org.postgresql.util.PSQLException: FATAL: sorry, too many clients already)

    我正在尝试连接到Postgresql数据库,我收到以下错误: 错误:org.postgresql.util. PSQLException:致命:抱歉,已经有太多客户 错误是什么意思,我该如何解决? 我 ...

  7. gstreamer的gst-inspect 和gst-launch

    用gstreamer架构做对媒体开发时,gst-inspect 和gst-launch是两个非常使用的小工具,前者是用于查询库中已经包含的所有element以及他们的详细信息,后者用于快速构建一条pi ...

  8. Vscode 保存文件就会自动添加注释

    Vscode 保存文件就会自动添加注释   原因是:安装了插件造成的..   文章来源:刘俊涛的博客 欢迎关注公众号.留言.评论,一起学习. _____________________________ ...

  9. (待续)【转载】 DeepMind发Nature子刊:通过元强化学习重新理解多巴胺

    原文地址: http://www.dataguru.cn/article-13548-1.html -------------------------------------------------- ...

  10. idea-debug启动会卡住不动,BeanPostProcessors (for example: not eligible for auto-proxying),报错解决

    debug启动会卡住不动,run模式启动正常 debug启动输出到下面这行之后,就不会继续输出了 爆出各种 [INFO]- Bean 'dataSource' of type [class Druid ...