java 计算 时间差_Java8 时间差计算

方式一:

/**

* DateTimeFormatter java 8 线程安全

* @throws Exception

*/

@Test

public void testTimeDiff() throws Exception {

// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

LocalDateTime startDate = LocalDateTime.parse("2019-07-12 00:00:00", formatter);

LocalDateTime endDate = LocalDateTime.parse("2019-10-12 02:00:00", formatter);

// 日期区间

long days = ChronoUnit.DAYS.between(startDate, endDate);

//月

long month = ChronoUnit.MONTHS.between(startDate, endDate);

//年

long year = ChronoUnit.YEARS.between(startDate, endDate);

long hour = ChronoUnit.HOURS.between(startDate, endDate);

long min = ChronoUnit.MINUTES.between(startDate, endDate);

System.err.println( "相差MIN:"+min);

System.err.println( "相差hour:"+hour);

System.err.println( "相差days :"+days);

System.err.println( "相差month :"+month);

System.err.println( "相差year:"+year);

}

public void test() {

try {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date now = df.parse("2019-03-26 13:31:40");//当前时间

Date date = df.parse("2004-01-02 11:30:24");//过去

long l = now.getTime() - date.getTime();

long day = l / (24 * 60 * 60 * 1000);

long hour = (l / (60 * 60 * 1000) - day * 24);

long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);

long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

System.out.println("" + day + "天" + hour + "小时" + min + "分" + s + "秒");

} catch (Exception e) {

}

}

标签:startDate,endDate,hour,System,long,时间差,60,计算,Java8

来源: https://www.cnblogs.com/lshan/p/12493313.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值