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. SqlServer2012自增主键跳跃增长的问题解决方案

    1.问题:SqlServer2012自增主键插入几条数据,然后重启服务,然后再插入几条数据,发现重启后插入的记录ID出现跳跃. 2.解决方案: Open SQLServer configuration ...

  2. python 3使用binascii方法的报错解决

    环境是python 3 问题: 使用binascii方法一直出现报错TypeError: a bytes-like object is required, not 'str' #coding: utf ...

  3. Echart 仪表盘和柱形图

    我们来分布讲解: 1.首先编一写一个html,如下: <html> <body class=""> <div class="containe ...

  4. Python——XPath使用

    一:XPath介绍 XPath全称XML路径语言,用于确定XML文档中某部分位置.XPath基于XML树状结构,在树中寻找结点. 现在,一般使用XPath在XML中查找.提取信息,同时,它也支持HTM ...

  5. Xamarin.Android之ListView和Adapter

    一.前言 如今不管任何应用都能够看到列表的存在,而本章我们将学习如何使用Xamarin去实现它,以及如何使用适配器和自定义适配器(本文中的适配器的主要内容就是将原始的数据转换成了能够供列表控件显示的项 ...

  6. mysql5.6特殊字符问题

    问题描述: 在搭建redis监控cache-cloud软件,发现对建立cache-cloud的库,无法删除 drop database cache-cloud; 很奇怪..... 问题解决: 百思不得 ...

  7. Ubuntu18.04使用f3probe检测U盘实际容量

    项目主页 https://fight-flash-fraud.readthedocs.io/ 使用f3probe 能快速检测出被测U盘的实际容量, 命令 $ sudo f3probe --destru ...

  8. Ubuntu16.04下的stm32环境配置

    安装stlink 必须安装libusb-1.0-0-dev, 其他安装不起作用 -dev git clone https://github.com/texane/stlink.git cd stlin ...

  9. EntityFramework 5.0 CodeFirst 教程01-搭建环境和快速上手

    ----------------------------目录------------------------------ EntityFramework 5.0 CodeFirst 教程03-数据结构 ...

  10. uitableview做九宫格

    1:创建实体 #import <Foundation/Foundation.h> @interface Shop : NSObject @property (nonatomic, copy ...