原则上应该适用Period来计算,因为他是专门为这种需求设计的。当时他只能计算到两个时间差的,年月日

传入参数Period.between(LocalDate,LocalDate)

这里是计算两个LocalDateTime时间差的方法。

    @Test
public void test2()
{
LocalDateTime fromDateTime = LocalDateTime.of(1984, 12, 16, 7, 45, 55);
LocalDateTime toDateTime = LocalDateTime.of(2014, 9, 10, 6, 40, 45); LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime ); long years = tempDateTime.until( toDateTime, ChronoUnit.YEARS);
tempDateTime = tempDateTime.plusYears( years ); long months = tempDateTime.until( toDateTime, ChronoUnit.MONTHS);
tempDateTime = tempDateTime.plusMonths( months ); long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS);
tempDateTime = tempDateTime.plusDays( days ); long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS);
tempDateTime = tempDateTime.plusHours( hours ); long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES);
tempDateTime = tempDateTime.plusMinutes( minutes ); long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS); System.out.println( years + " 天 " +
months + " 月 " +
days + " 天 " +
hours + " 时 " +
minutes + " 分 " +
seconds + " 秒.");
}

输出效果

转载自:https://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime

java8中计算两个日期时间LocalDateTime的时间差,格式化成xx年yy月zz日aa时bb分cc秒的更多相关文章

  1. Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数

    格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: /** * 获取指定时间到格林威治时间的秒数 ...

  2. JAVA中计算两个日期时间的差值竟然也有这么多门道

    上半年春招的时候,作为面试官,对于面试表现的不错的同学会要求其写一小段代码看看.题目很简单: 给定一个日期,然后计算下距离今天相差的天数. 本以为这么个问题就是用来活跃面试氛围的,但是结果却让人大跌眼 ...

  3. SQL Server中如何获取当前年,月,日,时,分,秒

    分类: SQL Server  select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) ...

  4. SQLServer中取当前年,月,日,时,分,秒

    Select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName ...

  5. oracle中extract()函数----用于截取年、月、日、时、分、秒

    oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分 语法如下: extract ( { year | month | day ...

  6. java7与java8中计算两个日期间隔多少年多少月多少天的实现方式

    最近工作中碰到个新需求,计算每个员工入职公司的时长,要求形式为多少年多少月多少天形式,某个值为0就跳过不显示,因为前段时间学习过java8新特性,对于这个需求,java8的新时间日期API可以直接解决 ...

  7. Oracle中计算两个日期时间的差

    --方法1 select floor((sysdate - to_date('2006-09-01 08:00:00', 'yyyy-mm-dd hh24:mi:ss'))) as sDays fro ...

  8. SQL SERVER数据库,按年、月、日、时、分、秒计算两个时间字段之间的间隔时间样例

    使用DATEDIFF(取值,时间字段1,时间字段2) 举例: SELECT DATEDIFF(YEAR,DRYSJ,DCYSJ),* FROM YXHIS2019..TBZYBR2019 --SQL ...

  9. sql 计算两时间或日期 的相差的 年、 月、 日、时、分、秒,年、月、日分别的提取

    --年.月.日.时.分.秒 datediff(yy,date1,date2) datediff(m,date1,date2) datediff(d,date1,date2) datediff(hh,d ...

随机推荐

  1. yum用法笔记

    yum是指通过linux系统的一个命令也是一个软件包管理工具,基于rpm管理,通过命令下载指定网站的包源,下载好之后自动解压和分配 下载yum:一般linux的服务器在出厂前都自带yum,包括虚拟机 ...

  2. C++——宏观把控

    跟看所有的书一样,我们都要求第一遍泛读,宏观把控书本内容,C++依旧如此进行.看到前面这几章的时候感觉非常熟悉,因为能让我联想到很多以前学习的VB.C#等的知识,感觉轻松很多,原来我已经学过了很多东西 ...

  3. Mac上使用brew安装Nginx服务器

    使用brew安装nginx $ brew install nginx 启动nginx sudo nginx 访问localhost:8080 发现已出现nginx的欢迎页面了.  为方便期间,做个软 ...

  4. MySQL之left jion 、 right jion 和inner jion 的区别和使用方法

    left jion   左联结 right jion 右联结 inner jion 等值联结 create table teacher( tid ) primary key auto_incremen ...

  5. css 学习笔记 菜鸟

    1 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器通常是您需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成.每个属性有一个值.属性和值被冒号分开.CSS声明总是 ...

  6. SQL 学习指南-数据库使用

    1.缺失子句 now() 是MySQL的内建函数,返回当前的日期和时间.在MySQL中可以直接使用下列语句查询: SELECT NOW(); 但是某些数据库规定查询语句必须包含 from 子句,并在其 ...

  7. Number of Islands II

    Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Origina ...

  8. Linux ntp 时间同步服务配置

    一.基础环境 1.操作系统:CentOS 7.3 2.ntp:4.2.6 3.机器,服务端(192.168.1.210)客户端(192.168.1.211) 二.安装ntp yum -y instal ...

  9. exam8.29

    咕了好几篇后... 我终于开始重新写了 T1: 不会,没思路,暴搜还可能会(一开始我以为暴搜时间复杂度为$\Theta (mn ^ k)$) 于是码出了暴搜... 跑一遍$(4,4,5)$,然后... ...

  10. Cogs 750. 栅格网络(对偶图)

    栅格网络流 ★★☆ 输入文件:flowa.in 输出文件:flowa.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] Bob 觉得一般图的最大流问题太难了,他不知道如何解决, ...