private static final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");/*** 利用joda_time包计算** @param dateTime1 时间1* @param dateTime2 时间2* @return 两个时间的月份差值*/private static int getMonthDifferent(DateTime dateTime1, DateTime dateTime2) {return Months.monthsBetween(dateTime1, dateTime2).getMonths();}/*** 利用joda_time包计算** @param dateTime1 时间1* @param dateTime2 时间2* @return 两个时间的自然天数差值*/private static int getDaysDifferent(DateTime dateTime1, DateTime dateTime2) {return Days.daysBetween(dateTime1, dateTime2).getDays();}/*** 利用joda_time包计算两个时间的天数差** @param dt1 时间1* @param dt2 时间2* @return 天数差*/private static int getDaysDifferent(Date dt1, Date dt2) {DateTime dateTime1 = new DateTime(dt1);DateTime dateTime2 = new DateTime(dt2);return Days.daysBetween(dateTime1, dateTime2).getDays();}/*** 通过Calendar类得日期比较,在这需要考虑闰年和平年,也要考虑跨年份** @param date1 时间1* @param date2 时间2* @return 两个时间的自然天数差值*/public static int differentDays(Date date1, Date date2) {Calendar cal1 = Calendar.getInstance();cal1.setTime(date1);Calendar cal2 = Calendar.getInstance();cal2.setTime(date2);int day1 = cal1.get(Calendar.DAY_OF_YEAR);int day2 = cal2.get(Calendar.DAY_OF_YEAR);int year1 = cal1.get(Calendar.YEAR);int year2 = cal2.get(Calendar.YEAR);if (year1 != year2) {  //不同一年int timeDistance = 0;for (int i = year1; i < year2; i++) {if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { //闰年timeDistance += 366;} else { //不是闰年timeDistance += 365;}}return timeDistance + (day2 - day1);} else { //同一年return day2 - day1;}}/*** LocalDateTime 计算两个时间的时间差** @param dt1 时间1* @param dt2 时间2* @return 两个时间的月份差值*/private static int getMonthDifferent(LocalDateTime dt1, LocalDateTime dt2) {//获取第一个时间点的月份int month1 = dt1.getMonthValue();//获取第一个时间点的年份int year1 = dt1.getYear();//获取第一个时间点的月份int month2 = dt2.getMonthValue();//获取第一个时间点的年份int year2 = dt2.getYear();//返回两个时间点的月数差return (year2 - year1) * 12 + (month2 - month1);}/*** 计算月份差** @param time1 时间1* @param time2 时间2* @return 两个时间的月份差值*/private static int getMonthDifferent(Integer time1, Integer time2) {LocalDateTime startTime = LocalDateTime.ofEpochSecond(time1, 0, ZoneOffset.ofHours(8));LocalDateTime endTime = LocalDateTime.ofEpochSecond(time2, 0, ZoneOffset.ofHours(8));return getMonthDifferent(startTime, endTime);}public static void main(String[] args) {DateTime dateTime1 = formatter.parseDateTime("2018-01-12");DateTime dateTime2 = formatter.parseDateTime("2019-12-25");int months = getMonthDifferent(dateTime1, dateTime2);System.out.println("1、月份差:" + months);LocalDateTime of1 = LocalDateTime.of(2018, 1, 12, 1, 1);LocalDateTime of2 = LocalDateTime.of(2019, 12, 25, 23, 16);int monthDiff = getMonthDifferent(of1, of2);System.out.println("2、月份差:" + monthDiff);int daysDifferent = getDaysDifferent(dateTime1, dateTime2);System.out.println("1、天数差:" + daysDifferent);int days = differentDays(new Date(2018 , 1 , 12), new Date(2019 , 12 ,25));System.out.println("2、天数差:" + days);Date date = Date.from(LocalDate.of(2016, 2, 1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());Date date2 = Date.from(LocalDate.of(2018, 3, 1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());int different = getDaysDifferent(date, date2);System.out.println("3、天数差:" +different);}

运行结果:

使用joda_time计算两时间的月数差,天数差相关推荐

  1. java 计算两个日期相差月数_Java简单计算两个日期月数差的方法

    本文实例讲述了Java简单计算两个日期月数差的方法.分享给大家供大家参考,具体如下: /** * 获取两个日期相差的月数 * @param d1 较大的日期 * @param d2 较小的日期 * @ ...

  2. Oracle 计算两日期间隔月数

    Oracle 计算两日期之间月数 实际应用中,有时候会计算两日期间隔天数.月数.如直接相减,可能会出问题,如: select 202110-202106 from dual; 结果为:4 看似正确,但 ...

  3. Java计算两个字符串日期之间的天数差

    Java计算两个字符串日期之间的天数差 调用方法: public static void main(String[] args) throws ParseException {String a = & ...

  4. 两个日期月数差值计算

    1. 场景 在金融类项目中,经常会涉及到利息计算,这里讨论一下按月计息的情况下,月数如何计算 2. 需求分析 前提条件,计算日期差值的时候,留头去尾,举例01.01-01.02差值为1天 月数其实算起 ...

  5. JS-计算日期差值;计算日期之间的月数

    计算两天之间的日期差值 // 输入格式:yyyy-MM-DD function daysBetween(sDate1, sDate2) {//Date.parse() 解析一个日期时间字符串,并返回1 ...

  6. MySQL日期当前时间加月数

    MySQL日期当前时间加月数 SQL 日期: 当我们处理日期时,最难的任务恐怕是确保所插入的日期的格式,与数据库中日期列的格式相匹配. 只要数据包含的只是日期部分,运行查询就不会出问题.但是,如果涉及 ...

  7. java计算两个日期之间相差的天数的四种方法

    计算两个日期之间相差的天数的四种方法 第一种:时间戳的方式,计算两个日期的时间戳的差,再除当天的毫秒数即可得到相差的天数. public static void main(String[] args) ...

  8. C#计算两个日期之间相差的天数

    C#计算两个日期之间相差的天数 private int DateDiff(DateTime dateStart, DateTime dateEnd) { DateTime start = Conver ...

  9. 计算两个时间戳之间的自然天数

    2019独角兽企业重金招聘Python工程师标准>>> 这两天需要计算两个时间戳之间相差的天数,以前很随意自然的想法就是两个数字相减,然后除以一天的秒数86400,但是这样反复测试都 ...

最新文章

  1. 索引的使用—— 验证索引提升查询效率 || 避免索引失效 —— 全值匹配 /最左前缀法则/范围查询右边的列,不能使用索引/不要在索引列上进行运算操作/字符串不加单引号,造成索引失效
  2. java中的取模_Java 中的取模和取余
  3. Sony如何启动微型计算机,索尼笔记本怎么进入bios 索尼VAIO笔记本进入bios技巧
  4. [Linux] 使用openssl实现RSA非对称加密
  5. [BZOJ 1834] [ZJOI2010]network 网络扩容
  6. React Native 开发豆瓣评分(六)添加字体图标
  7. Spring学习总结(14)——Spring10种常见异常解决方法
  8. bzoj 1694 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草(DP)
  9. [转载] python——连接Oracle数据库
  10. 手机 测试php代码大全,VIVO手机测试指令代码大全
  11. 高等代数-三-消元法
  12. html消除样式,清除css样式
  13. java物流bos,宅急送BOS物流项目
  14. Gossip协议笔记--谣言、流行病协议
  15. SVG_16_defs标签_use标签_style标签_红绿灯效果
  16. gnome 如何自定义样式_在Gnome 3中自定义字体
  17. 玩凤凰沙盘的几点思考
  18. 网络爬虫在电商定价策略上的应用
  19. 国内外6款优秀的免费CDN服务
  20. 这是一个最好的时代,也是一个最坏的时代 ?

热门文章

  1. 百度编辑器ueditor添加视频方法
  2. 模取幂运算 计算a^b mod n
  3. 贪心算法解决找零钱问题
  4. junit5 mock静态方法 Mockito.mockStatic()
  5. java ea mql4_【MQL4】开发EA前需对EA的策略进行规划
  6. 定时任务 Corn表达式
  7. (万年历一)JAVA在控制台输出万年历(1900年起)
  8. php获取网站截图,异步获取评论者网站截图
  9. Opencv 图片处理
  10. 主要的技术Blog网站