转:Java 计算2个时间相差多少年,多少个月,多少天的几种方式
日期比较对象 DayCompare 代码用到了 lombok ,如果不用,其实就是把getter / setter方法自己写一遍,还有构造方法。
@Data
@Builder
public static class DayCompare{
private int year;
private int month;
private int day;
}
/**
* 计算2个日期之间相差的 相差多少年月日
* 比如:2011-02-02 到 2017-03-02 相差 6年,1个月,0天
* @param fromDate
* @param toDate
* @return
*/
public static DayCompare dayComparePrecise(Date fromDate,Date toDate){
Calendar from = Calendar.getInstance();
from.setTime(fromDate);
Calendar to = Calendar.getInstance();
to.setTime(toDate); int fromYear = from.get(Calendar.YEAR);
int fromMonth = from.get(Calendar.MONTH);
int fromDay = from.get(Calendar.DAY_OF_MONTH); int toYear = to.get(Calendar.YEAR);
int toMonth = to.get(Calendar.MONTH);
int toDay = to.get(Calendar.DAY_OF_MONTH);
int year = toYear - fromYear;
int month = toMonth - fromMonth;
int day = toDay - fromDay;
return DayCompare.builder().day(day).month(month).year(year).build();
}
/**
* 计算2个日期之间相差的 以年、月、日为单位,各自计算结果是多少
* 比如:2011-02-02 到 2017-03-02
* 以年为单位相差为:6年
* 以月为单位相差为:73个月
* 以日为单位相差为:2220天
* @param fromDate
* @param toDate
* @return
*/
public static DayCompare dayCompare(Date fromDate,Date toDate){
Calendar from = Calendar.getInstance();
from.setTime(fromDate);
Calendar to = Calendar.getInstance();
to.setTime(toDate);
//只要年月
int fromYear = from.get(Calendar.YEAR);
int fromMonth = from.get(Calendar.MONTH); int toYear = to.get(Calendar.YEAR);
int toMonth = to.get(Calendar.MONTH); int year = toYear - fromYear;
int month = toYear * 12 + toMonth - (fromYear * 12 + fromMonth);
int day = (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / (24 * 3600 * 1000));
return DayCompare.builder().day(day).month(month).year(year).build();
}
/**
* 计算2个日期相差多少年
* 列:2011-02-02 ~ 2017-03-02 大约相差 6.1 年
* @param fromDate
* @param toDate
* @return
*/
public static String yearCompare(Date fromDate,Date toDate){
DayCompare result = dayComparePrecise(fromDate, toDate);
double month = result.getMonth();
double year = result.getYear();
//返回2位小数,并且四舍五入
DecimalFormat df = new DecimalFormat("######0.0");
return df.format(year + month / 12);
}
转自:https://www.sojson.com/blog/260.html
转:Java 计算2个时间相差多少年,多少个月,多少天的几种方式的更多相关文章
- java计算两个时间相差(天、小时、分钟、秒)
public static Long dateDiff(String startTime, String endTime, String format, String str) { // 按照传入的格 ...
- java计算两个日期相差多少天
java计算两个日期相差多少天 public class DateUtil{ public static int betweenDays(Date startDate, Date endDate ) ...
- Java集合(九)哈希冲突及解决哈希冲突的4种方式
Java集合(九)哈希冲突及解决哈希冲突的4种方式 一.哈希冲突 (一).产生的原因 哈希是通过对数据进行再压缩,提高效率的一种解决方法.但由于通过哈希函数产生的哈希值是有限的,而数据可能比较多,导致 ...
- PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组
PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 <?php /** * 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 * @param [type] $st ...
- java 判断两个时间相差的天数
1.实现目标 输入:两个日期 输出:两个日期相差的天数 2.代码实现 方法1: 通过Calendar类的日期比较.注意:这里需要考虑一下: 日期是跨年份的,如一个是2012年,一个是2015年的 ...
- Java判断两个时间相差的天数
1.实现目标 输入:两个日期 输出:两个日期相差的天数 2.代码实现 方法1: 通过Calendar类的日期比较.注意:这里需要考虑一下: 日期是跨年份的,如一个是2012年,一个是2015年的 ...
- js计算两个时间相差天数
//两个时间相差天数 兼容firefox chrome function datedifference(sDate1, sDate2) { //sDate1和sDate2是2006-12 ...
- Java计算两个时间的天数差与月数差 LocalDateTime
/** * 计算两个时间点的天数差 * @param dt1 第一个时间点 * @param dt2 第二个时间点 * @return int,即要计算的天数差 */ public stat ...
- java多线程系类:基础篇:02常用的实现多线程的两种方式
本章,我们学习"常用的实现多线程的2种方式":Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多 ...
随机推荐
- 右边根据左边的高度自动居中只需要两行CSS就可以完成
右边根据左边的高度自动居中只需要两行CSS就可以完成 <style type="text/css" > div{ display: inline-block; vert ...
- Python3之format
print('{0},{1}'.format('zhangk', 32)) print('{},{},{}'.format('zhangk','boy',32)) print('{name},{sex ...
- C#——工厂模式
之前我们接介绍了简单工厂,这次我们介绍一种更为常用的模式——工厂模式. 工厂方法模式Factory Method,又称多态性工厂模式.在工厂方法模式中,核心的工厂类不再负责所有的产品的创建,而是将具体 ...
- 一款批量linux管理工具batchshell
BatchShell是什么? BatchShell是一款基于SSH2的批量文件传输及命令执行工具,它可以同时传输文件到多台远程服务器以及同时对多台远程服务器执行命令.BatchShell基于原生的sh ...
- Split()函数
最近遇到一个有趣的问题关于使用Split函数 ,该函数能够根据传递的参数拆分,并返回一个string的数组. 贴出一个奇怪的例子 using System; using System.Collecti ...
- ionic错误
1. 问题:Error: read ECONNRESET 启动使用ionic serve启动服务器之后只要一刷新界面就会导致服务器关闭,报的错误如下: events.js:136 throw er; ...
- Xftp 5 和 Xshell 5 基本使用方法
软件介绍: (1)Xshell: 一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的 TELNET 协议.Xshell通过互联网可以连接到远程的服 ...
- ubuntu14.0开机guest账号禁用方法
在终端里进入/usr/share/lightdm/lightdm.conf.d/目录 sudo vim 50-unity-greeter.conf 然后在文件里输入: [SeatDefaults] a ...
- Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method fail
SpringBoot 单元测试报错 @RunWith(SpringRunner.class) @SpringBootTest public class ProductCategoryRepositor ...
- chromeDriver下载地址
http://chromedriver.storage.googleapis.com/index.html