Java计算两个日期相差的年数、月数、天数
作者:互联网
1 借助hutool工具类
hutool maven依赖
1 <dependency> 2 <groupId>cn.hutool</groupId> 3 <artifactId>hutool-all</artifactId> 4 <version>5.1.0</version> 5 </dependency>
计算月数代码实现:
1 Date startDate = new Date(1553443200000L); 2 Date endDate = new Date(System.currentTimeMillis()); 3 long betweenMonth = DateUtil.betweenMonth(start, end, true); 4 System.out.println(betweenMonth);
2 java8实现
1 String text1 = "2019-03-25"; 2 Temporal startDate = LocalDate.parse(text1); 3 String text2 = "2022-05-30"; 4 Temporal endDate = LocalDate.parse(text2); 5 // 方法返回为相差月份 6 long l = ChronoUnit.MONTHS.between(startDate, endDate); 7 System.out.println(l);
标签:startDate,Java,月数,hutool,年数,System,betweenMonth,Date,endDate 来源: https://www.cnblogs.com/ming-blogs/p/16327516.html