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. openfiler在esxi下的安装配置

    注意分区的时候如果硬盘太小自动分区会导致分配的卷大小不够用 后改为如下: 以root登录: 应该以openfiler登录,口令是password 也可以导入虚拟机安装 升级虚拟机硬件版本 终端登录用户 ...

  2. 1051: 手机(MOBILE)

    #include <iostream> #include <iomanip> #include <cstdlib> #include <string> ...

  3. editcap的使用

    editcap.exe -h Editcap (Wireshark) (v2.-gf42a0d2b6c) Edit and/or translate the format of capture fil ...

  4. 使用jquery的ajax提交文件上传

    以前的项目大多的使用jquery的插件来进行文件上传,对于就只引用jquery而不使用插件来上传文件之前未有写过,最近项目里有写到和用到,就记录一下,以后方便查找. 提示:存在浏览器皆容问题,谨慎使用 ...

  5. RabbitMQ学习笔记2-理解消息通信

    消息包含两部分:1.有效载荷(payload) - 你想要传输的数据.2.标签(lable) - 描述有效载荷的相关信息,包含具体的交换器.消息的接受兴趣方等. rabbitmq的基础流程如下: Ra ...

  6. 改变mysql客户端输出的字符串编码

    在客户端改变中文输出的编码,通常以gbk输出,因为电脑常见的是gbk编号形式 目的:不改变编码,输出中文的时候,可能会出现乱码的情况, set names gbk 在客户端以gbk编码显示需要输出的内 ...

  7. 用python导出csv文件

    # coding: utf-8 import csv csvfile = open('csv.csv', 'wb') #打开方式还可以使用file对象 writer = csv.writer(csvf ...

  8. excel快速访问工具栏和自定义选项卡

    自定义选项卡: excel命令选项--自定义功能区--

  9. RHEL SHELL快捷键

    Shell快捷键 CTRL+a  调到命令行头  e  调到命令行尾 CTRL+u  光标前面的删除  k  光标后面的删除 CTRL+→词的头   词的尾 ESC+.   粘贴上个命令的尾词 杀掉远 ...

  10. Linux下wc命令统计文件行数/词数/字符数/最长行字符数

    wc命令帮助 $ wc --help Usage: wc [OPTION]... [FILE]... or: wc [OPTION]... --files0-from=F Print newline, ...