日期比较对象 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. NSNotificationCenter 的使用详解

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad.Apple 还为我们提供了另一种通知响应方式,那就是 NSNotif ...

  2. Laravel5.1学习笔记i14 系统架构6 Facade

    Facades 介绍  使用 Facades Facade 类参考   #介绍 Facades provide a "static" interface to classes th ...

  3. Unity 引擎UGUI之自定义树形菜单(TreeView)

    先上几张效果图:          如果你需要的也是这种效果,那你就来对地方了! 目前,我们这个树形菜单展现出来的功能如下: 1.可以动态配置数据源: 2.点击每个元素的上下文菜单按钮(也就是图中的三 ...

  4. Centos6.7 安装Naigos教程

    Centos6.7 安装Naigos教程参考文档:https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/quickst ...

  5. Python_多线程1(创建线程,简单线程同步)

    threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法: threading.currentThread(): 返回当前的线程变量. threading.enumera ...

  6. STL_string用法总结

    参考自:http://blog.csdn.net/y990041769/article/details/8763366 1:string对象的定义和初始化以及读写 string s1;      默认 ...

  7. day04-交互、格式化输出及基本运算符

    目录 与用户交互 python2和python3交互的区别 格式化输出 1 字符串拼接 2 占位符 3 format格式 4 f-string格式 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑 ...

  8. 北京Python开发培训怎么选?

    北京的地理优势和经济优势基本无需多言,作为全国机会最多的地方,吸引了无数的北漂前赴后继.作为中国互联网中心之一,北京有海量Python岗位正在等待大家淘金. 近几年中,Python一直是市场上最受欢迎 ...

  9. GFS分布式文件系统脚本

    #!/bin/bashfor i in $(fdisk -l | grep -wo "/dev/sd[b-z]" | sort)dodd if=/dev/zero of=$i bs ...

  10. 踩过的坑:__file__、__package__和__name__

    不说废话,直接上示例结构图 Path.py内容如下: import os path1 = os.path.dirname(os.path.abspath(__file__)) path2 = os.p ...