Java 8 – Period and Duration examples
Few examples to show you how to use Java 8 Duration, Period and ChronoUnit objects to find out the difference between dates.
- Duration – Measures time in seconds and nanoseconds.
- Period – Measures time in years, months and days.
1. Duration Example
A java.time.Duration example to find out difference seconds between two LocalDateTime
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
A java.time.Period example to find out differently (years, months, days) between two LocalDates
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 :
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的更多相关文章
- Java日期时间API系列9-----Jdk8中java.time包中的新的日期时间API类的Period和Duration的区别
1.Period final修饰,线程安全,ISO-8601日历系统中基于日期的时间量,例如2年3个月4天. 主要属性:年数,月数,天数. /** * The number of years. */ ...
- 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 ...
- 微博开发平台java SDK demo学习之examples(demo)
本文介绍demo中函数的功能分块 账号 评论 收藏 关系/好友分组 地理信息 OAuth2(开发指南) 位置服务(开发指南)
- Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解
前言 java 8 中引入的两个与日期相关的新类:Period 和 Duration.两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值.他们 ...
- Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式
要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...
- [转载]Java 8 日期&时间 API
Java 8 日期和时间 声明 本文转自http://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant,以mark ...
- 【转】JAVA 8 日期/时间(Date Time)API指南
前言 本来想写下Java 8的日期/时间API,发现已经有篇不错的文章了,那就直接转载吧~ PS:主要内容没变,做了部分修改. 原文链接: journaldev 翻译: ImportNew.com - ...
- 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 ...
- 20145205 《Java程序设计》第7周学习总结
教材学习内容总结 认识时间与日期 1.格林威治时间(GMT):通过观察太阳而得,因为地球公转轨道为椭圆形且速度不一,本身自传减速而造成误差. 2.世界时(UT):通过观测远方星体跨过子午线而得,受地球 ...
随机推荐
- 微软BI 之SSAS 系列 - 基于雪花模型的维度设计
基于雪花模型的维度以下面的 Product 产品与产品子类别,产品类别为例. DimProduct 表和 DimProductSubcategory 表有外键关系,而 DimProductSubcat ...
- 1004: 不明飞行物(ufo)
#include <iostream> #include <iomanip> #include <cstdlib> #include <string> ...
- oneinstack一键部署linux生产环境那点事(ubuntu)
http://oneinstack.com/install/ (1)将oneinstack-full.tar.gz最新版安装文件上传至/usr/local/下 (2)解压tar xzvf oneins ...
- scala VS python2 操作shell对比例子
Scala: /** * Created by sunxu on 2015/9/30. */ import scala.sys.process._ import java.io.File //在相应目 ...
- SLF4J warning or error messages and their meanings
来自:http://www.slf4j.org/codes.html#StaticLoggerBinder The method o.a.commons.logging.impl.SLF4FLogFa ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- (转)C#中Invoke的用法 一
在用.NET Framework框架的WinForm构建GUI程序界面时,如果要在控件的事件响应函数中改变控件的状态,例如:某个按钮上的文本原先叫“打开”,单击之后按钮上的文本显示“关闭”,初学者往往 ...
- java面试第二天
局部变量:不是声明在类体括号里面的变量 (1)必须要先赋值,后使用,否则通不过编译,局部变量没有默认初始化值 (2)作用范围:定义开始到定义它的代码块结束 (3)同一范围内,不允许2个局部变量命名冲突 ...
- percona-Toolkit
1:下载最新安装包 wget https://www.percona.com/downloads/percona-toolkit/2.1.1/percona-toolkit-2.1.1.tar.gz ...
- 〖Network〗宿舍配置两路由器,同时访问校园内网和校园外网
环境: 校园宿舍, 10.x.x.x 和 172.16.x.x~172.31.x.x是校园内网,本科教务系统什么的都在上边 路由器: 路由器1(校园内网):水星MR807 路由器2(拨号上网):TP ...