日期比较对象 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个时间相差多少年,多少个月,多少天的几种方式的更多相关文章

  1. java计算两个时间相差(天、小时、分钟、秒)

    public static Long dateDiff(String startTime, String endTime, String format, String str) { // 按照传入的格 ...

  2. java计算两个日期相差多少天

    java计算两个日期相差多少天 public class DateUtil{ public static int betweenDays(Date startDate, Date endDate ) ...

  3. Java集合(九)哈希冲突及解决哈希冲突的4种方式

    Java集合(九)哈希冲突及解决哈希冲突的4种方式 一.哈希冲突 (一).产生的原因 哈希是通过对数据进行再压缩,提高效率的一种解决方法.但由于通过哈希函数产生的哈希值是有限的,而数据可能比较多,导致 ...

  4. PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组

    PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 <?php /** * 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 * @param [type] $st ...

  5. java 判断两个时间相差的天数

    1.实现目标 输入:两个日期 输出:两个日期相差的天数 2.代码实现 方法1: 通过Calendar类的日期比较.注意:这里需要考虑一下: 日期是跨年份的,如一个是2012年,一个是2015年的   ...

  6. Java判断两个时间相差的天数

    1.实现目标 输入:两个日期 输出:两个日期相差的天数 2.代码实现 方法1: 通过Calendar类的日期比较.注意:这里需要考虑一下: 日期是跨年份的,如一个是2012年,一个是2015年的   ...

  7. js计算两个时间相差天数

     //两个时间相差天数 兼容firefox chrome    function datedifference(sDate1, sDate2) {    //sDate1和sDate2是2006-12 ...

  8. Java计算两个时间的天数差与月数差 LocalDateTime

    /**  * 计算两个时间点的天数差  * @param dt1 第一个时间点  * @param dt2 第二个时间点  * @return int,即要计算的天数差  */ public stat ...

  9. java多线程系类:基础篇:02常用的实现多线程的两种方式

    本章,我们学习"常用的实现多线程的2种方式":Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多 ...

随机推荐

  1. JS——祝愿墙

    注意事项: 1.for循环的下一层注册了事件的话,事件函数中关于变量i的节点元素是不允许出现的,因为在函数加载的时候,只会加载函数名,不会加载函数体,外层for循环会走完一边,变量i一直会停留在最后一 ...

  2. http服务器与https服务器的区别

    1.HTTPS服务器使用的是HTTPS协议,而HTTP使用的是HTTP协议. 2.HTTPS服务器需要向证书授权中心申请证书,一般免费证书很少,需要交费. 3.HTTP服务器与客户端传递的是明文数据, ...

  3. 中望CAD VBA检测文件是否存在

    Option Explicit Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "Path ...

  4. Render2

    https://blog.csdn.net/wf19930209/article/details/81109388

  5. Photoshop如何实现图片相对画布居中

    先按ctrl+A,再选择要居中的图层,然后就会发现居中按钮被激活了

  6. On branch master nothing to commit, working tree clean ERROR: Repository not found. fatal: Could not read from remote repository.

    将gitbash部署hexo到github:hexo deploy 报以下错误: Administrator@liu MINGW64 /Hexo $ hexo d INFO Deploying: gi ...

  7. parseInt()函数

    parseInt()转化整形是从左往右,取出第一个整型,如:10a10b,则显示10: //20170719补充 如果该字符串第一个字符是0,那么该字符串会基于八进制而非十进制来求值,在八进制中,8和 ...

  8. join 和 left join 和 right join的区别?

    join等价于 inner join 是内连接 ,返回两个表都有的符合条件的行. left join 是左连接,返回坐表中所有的行以及右表中符合条件的行. right join右连接,是返回右表中所有 ...

  9. 点击 table 单元格 取值

    function Test() { var rows = document.getElementById("tbDetail").rows; if (rows.length > ...

  10. C# Thu Mar 1 00:00:00 UTC+0800 2012 如何转换为2012-03-01

    string s = "Thu Mar 1 00:00:00 UTC+0800 2012"; DateTime dt = DateTime.ParseExact(s, " ...