Few examples to show you how to use Java 8 DurationPeriod and ChronoUnit objects to find out the difference between dates.

  1. Duration – Measures time in seconds and nanoseconds.
  2. Period – Measures time in years, months and days.

1. Duration Example

java.time.Duration example to find out difference seconds between two LocalDateTime

DurationExample.java
package com.mkyong.time;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit; public class DurationExample { public static void main(String[] args) { // Creating Durations
System.out.println("--- Examples --- "); Duration oneHours = Duration.ofHours(1);
System.out.println(oneHours.getSeconds() + " seconds"); Duration oneHours2 = Duration.of(1, ChronoUnit.HOURS);
System.out.println(oneHours2.getSeconds() + " seconds"); // Test Duration.between
System.out.println("\n--- Duration.between --- "); LocalDateTime oldDate = LocalDateTime.of(2016, Month.AUGUST, 31, 10, 20, 55);
LocalDateTime newDate = LocalDateTime.of(2016, Month.NOVEMBER, 9, 10, 21, 56); System.out.println(oldDate);
System.out.println(newDate); //count seconds between dates
Duration duration = Duration.between(oldDate, newDate); System.out.println(duration.getSeconds() + " seconds"); }
}

Output

--- Examples ---
3600 seconds
3600 seconds --- Duration.between ---
2016-08-31T10:20:55
2016-11-09T10:21:56
6048061 seconds

2. Period Example

java.time.Period example to find out differently (years, months, days) between two LocalDates

PeriodExample.java
package com.mkyong.time;

import java.time.LocalDate;
import java.time.Month;
import java.time.Period; public class PeriodExample { public static void main(String[] args) { System.out.println("--- Examples --- "); Period tenDays = Period.ofDays(10);
System.out.println(tenDays.getDays()); //10 Period oneYearTwoMonthsThreeDays = Period.of(1, 2, 3);
System.out.println(oneYearTwoMonthsThreeDays.getYears()); //1
System.out.println(oneYearTwoMonthsThreeDays.getMonths()); //2
System.out.println(oneYearTwoMonthsThreeDays.getDays()); //3 System.out.println("\n--- Period.between --- ");
LocalDate oldDate = LocalDate.of(1982, Month.AUGUST, 31);
LocalDate newDate = LocalDate.of(2016, Month.NOVEMBER, 9); System.out.println(oldDate);
System.out.println(newDate); // check period between dates
Period period = Period.between(oldDate, newDate); System.out.print(period.getYears() + " years,");
System.out.print(period.getMonths() + " months,");
System.out.print(period.getDays() + " days"); }
}

Output

--- Examples ---
10
1
2
3 --- Period.between ---
1982-08-31
2016-11-09
34 years,2 months,9 days

3. ChronoUnit Example

Alternatively, you can use ChronoUnit.{unit}.between to find out the difference between dates, review the following example :

ChronoUnitExample.java
package com.mkyong.time;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit; public class ChronoUnitExample { public static void main(String[] args) { LocalDateTime oldDate = LocalDateTime.of(1982, Month.AUGUST, 31, 10, 20, 55);
LocalDateTime newDate = LocalDateTime.of(2016, Month.NOVEMBER, 9, 10, 21, 56); System.out.println(oldDate);
System.out.println(newDate); // count between dates
long years = ChronoUnit.YEARS.between(oldDate, newDate);
long months = ChronoUnit.MONTHS.between(oldDate, newDate);
long weeks = ChronoUnit.WEEKS.between(oldDate, newDate);
long days = ChronoUnit.DAYS.between(oldDate, newDate);
long hours = ChronoUnit.HOURS.between(oldDate, newDate);
long minutes = ChronoUnit.MINUTES.between(oldDate, newDate);
long seconds = ChronoUnit.SECONDS.between(oldDate, newDate);
long milis = ChronoUnit.MILLIS.between(oldDate, newDate);
long nano = ChronoUnit.NANOS.between(oldDate, newDate); System.out.println("\n--- Total --- ");
System.out.println(years + " years");
System.out.println(months + " months");
System.out.println(weeks + " weeks");
System.out.println(days + " days");
System.out.println(hours + " hours");
System.out.println(minutes + " minutes");
System.out.println(seconds + " seconds");
System.out.println(milis + " milis");
System.out.println(nano + " nano"); }
}

Output

1982-08-31T10:20:55
2016-11-09T10:21:56 --- Total ---
34 years
410 months
1784 weeks
12489 days
299736 hours
17984161 minutes
1079049661 seconds
1079049661000 milis
1079049661000000000 nano
http://www.mkyong.com/tutorials/spring-boot-tutorials/

Java 8 – Period and Duration examples的更多相关文章

  1. Java日期时间API系列9-----Jdk8中java.time包中的新的日期时间API类的Period和Duration的区别

    1.Period final修饰,线程安全,ISO-8601日历系统中基于日期的时间量,例如2年3个月4天. 主要属性:年数,月数,天数. /** * The number of years. */ ...

  2. Java 8 – Filter a Map examples

    Java 8 – Filter a Map examplesFew Java examples to show you how to filter a Map with Java 8 stream A ...

  3. 微博开发平台java SDK demo学习之examples(demo)

    本文介绍demo中函数的功能分块 账号 评论 收藏 关系/好友分组 地理信息 OAuth2(开发指南) 位置服务(开发指南)

  4. Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解

    前言 java 8 中引入的两个与日期相关的新类:Period 和 Duration.两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值.他们 ...

  5. Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式

    要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...

  6. [转载]Java 8 日期&时间 API

    Java 8 日期和时间 声明 本文转自http://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant,以mark ...

  7. 【转】JAVA 8 日期/时间(Date Time)API指南

    前言 本来想写下Java 8的日期/时间API,发现已经有篇不错的文章了,那就直接转载吧~ PS:主要内容没变,做了部分修改. 原文链接: journaldev 翻译: ImportNew.com - ...

  8. Java 8 Date Time API Example Tutorial – LocalDate, Instant, LocalDateTime, Parse and Format

    参考 Java 8 Date and Time API is one of the most sought after change for developers. Java has been mis ...

  9. 20145205 《Java程序设计》第7周学习总结

    教材学习内容总结 认识时间与日期 1.格林威治时间(GMT):通过观察太阳而得,因为地球公转轨道为椭圆形且速度不一,本身自传减速而造成误差. 2.世界时(UT):通过观测远方星体跨过子午线而得,受地球 ...

随机推荐

  1. 微信小程序 - 深度定义骨架屏(提示)

    此举每个页面必须创建对应的css样式,比较麻烦(但非常准确),推荐使用组件化的skeleton组件 原理很简单:知晓一下this.setData原理,就OK了,可能大家会因此了解到全屏加载loadin ...

  2. Zoning and LUN Masking

    In a SAN ( Storage Area Network ), if all the hosts are allowed to access all the drives in the SAN, ...

  3. JSP与Servlet之间的关系事例说明

    Servlet Servlet 没有 main 方法,不能够独立的运行,它的运行需要容器的支持,Tomcat 是最常用的 JSP/Servlet 容器.Servlet 运行在 Servlet 容器中, ...

  4. MAC LINUX 安装PYQT(事例)

    MAC安装 1.安装命令:brew install pyqt Warning: Your Xcode () is outdated Please install Xcode 5.0. Warning: ...

  5. Nutch的安装和配置

    Nutch是一个Java实现的网络爬虫.Nutch的安装可以使用二进制包,也可以使用源代码安装.这里介绍用二进制包安装. 1. 下载apache-nutch-1.12-bin.tar.gz,并且解压, ...

  6. llvm -O 经历过那些pass

    https://stackoverflow.com/questions/15548023/clang-optimization-levels

  7. java中经常使用的快捷键

    Eclipse(MyEclipse) 经常使用快捷键Eclipse 的编辑功能很强大.掌握了 Eclipse 快捷键功能,能够大大提高开发效率. Eclipse中有例如以下一些和编辑相关的快捷键.1. ...

  8. 9、redis之事务2-Jedis的八种调用方式(事务、管道、分布式)介绍

    1.普通同步 @Test public void test1Normal() { Jedis jedis = new Jedis("localhost"); long start ...

  9. python之模块datetime 常见操作

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块datetime #http://blog.sina.com.cn/s/blog_6c37 ...

  10. URI -URL-URN区别

    URI -URL-URN区别 URI (uniform resource identifier)   统一资源标识符,用来唯一的标识一个资源URL(uniform resource locator)  ...