Java 计算2个时间相差多少年,多少个月,多少天的几种方式

JSON 2017-12-21 01:23:36 173567

一、Java 时间比较需求

1.A时间到B时间,相差多少年,月,日。

如:2011-02-02 到  2017-03-02 ,结果为:

 *  相差 6年,1个月,0天

2.A时间到B时间, 相差年,月,日各是多少。

如:2011-02-02 到  2017-03-02,结果为:

 *  以年为单位相差为:6

 *  以月为单位相差为:73个月

 *  以日为单位相差为:2220

3.A时间到B时间,相差多少年

如:2011-02-02 到  2017-03-02,结果大约为:  

 * 6.1

这个需求体现在如我加入公司 3 年 6 个月,也可以说是3.5年。

不多说,看代码。

二、Java 时间代码实现

日期比较对象 DayCompare 代码用到了   lombok  回头会讲到。如果你不用,其实就是把getter / setter方法自己写一遍,还有构造方法。

@Data
@Builder
public static class DayCompare{
    private int year;
    private int month;
    private int day;
}

2.1 A时间到B时间,相差多少年,月,日。

/**
 * 计算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.2 A时间到B时间, 相差年,月,日各是多少

/**
 * 计算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.3 A时间到B时间,相差多少年

/**
 * 计算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);
}

因为写到了,就分享出来一下。


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/260.html

转载时必须以链接形式注明原始出处及本声明。

本文主题:

如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
Java SHA1的几种实现方式Java SHA1 签名
时间戳转换介绍以及常见的几种转换方式
Java获取浏览器请求头(User-Agent),分析浏览器信息,系统信息的几种办法。
dns-prefetch对网站速度能提升有多少?详解dns-prefetch。
Java客户端Jedis 对Redis的的八调用方式(事务、管道、分布式)介绍及测试
万年历—Java计算感恩节是哪天?感恩节是哪一?2017年感恩节是哪一
万年历—Java计算父亲节是哪天?父亲节是哪一?2017年的父亲节是哪一
万年历—Java计算母亲节是哪天?母亲节是哪一? 2017年母亲节是哪一
Java对文件CRC32值计算
Freemarker静态化加载模板的三方式
最新文章
PHP变量剖析 11
SQL全外连接剖析 119
SQL自然连接剖析 147
springboot启动原理 245
SQL右连接【RIGHT JOIN】详解及图解 450
SQL左链接【LEFT JOIN】详解及图解 357
SQL非等值连接剖析 262
SQL等链接剖析 291
SQL内连接详解及图解 385
python之numpy常用的100种数值相关方法及代码示例 231
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 679241
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 674561
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 599031
免费天气API,天气JSON API,不限次数获取十五天的天气预报 565278
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 551715
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 509186
Elasticsearch教程(四) elasticsearch head 插件安装和使用 479645
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 262820
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244092
Elasticsearch教程(一),全程直播(小白级别) 225127
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...