原则上应该适用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. P1078 文化之旅[最短路]

    题目背景 本题是错题,后来被证明没有靠谱的多项式复杂度的做法.测试数据非常的水,各种玄学做法都可以通过(比如反着扫),不代表算法正确.因此本题题目和数据仅供参考. 题目描述 有一位使者要游历各国,他每 ...

  2. matlab(5) : 求得θ值后用模型来预测 / 计算模型的精度

    求得θ值后用模型来预测 / 计算模型的精度  ex2.m部分程序 %% ============== Part 4: Predict and Accuracies ==============% Af ...

  3. Mac+appium+iOS 环境搭建

    Mac+appium+iOS 环境搭建,需要用到的信息如下,参考搭建环境. 1.安装brew,安装介绍:https://jingyan.baidu.com/article/fec7a1e5ec3034 ...

  4. 深入剖析TOMCAT

    理解tomcat之搭建简易http服务器 做过java web的同学都对tomcat非常熟悉.我们在使用tomcat带来的便利的同时,是否想过tomcat是如何工作的呢?tomcat本质是一个http ...

  5. 如何在C中定义多行宏定义?

    请参阅下面的示例,其中我将交换两个变量的值. do-while(0)结构很不错 #include <stdio.h> #define swap(x,y,T) do { \ T temp = ...

  6. DYNAMIC LINK LIBRARY - DLL

    https://www.tenouk.com/ModuleBB.html MODULE BB DYNAMIC LINK LIBRARY - DLL Part 1: STORY What do we h ...

  7. [Angular 8] Implement a Custom Preloading Strategy with Angular

    Preloading all modules is quite an extreme approach and might not always be desirable. For instance, ...

  8. Centos命令和Shell脚本问题集合

    1.cat 错误写法 cat >> somefile.txt << EOF something EOF 原因:EOF 之后一定要是 ENTER(回车) 不能是空格或者其他.EO ...

  9. sql server 将某一列的值拼成一个字符串 赋值到一个字段内

    DECLARE @refCodeitems VARCHAR(800),   SELECT @refCodeitems=ISNULL(@refCodeitems,'')+refCodeitem +'/' ...

  10. MongoDB 集群设置集合分片生效及查看集合分片情况

    一.设计DB分片与Collection分片 #连接mongos /opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo  127.0.0.1:27017   ...