博客
关于我
java计算两个时间段的重合天数
阅读量:663 次
发布时间:2019-03-15

本文共 3078 字,大约阅读时间需要 10 分钟。

由于我要计算一个合同在当月的分摊的金额,所以就要知道这个合同的有效期在本月有多少天,这就要进行两个时间段重合天数的计算。

两个时间段四个时间点,相当于时间轴上的两条线段(b代表起点,e代表端点,b<=e)和4个端点。

可分3种情况:

1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此时,重合天数为零。
2.相交。
  情况一:(b1---【b2---e1)----e2】                 if(b1<b2&&e1<e2&&e1>b2)
  情况二:【b2---(b1---e2】----e1)           if(b1>b2&&b1<e2&&e2<e1)
3.包含:计算较短的时间段日期长度。
 (b1---【b2-----e2】--e1)               if(b1<b2&&e1>e2)
【b2---(b1-----e1)--e2】               if(b1>b2&&e1<e2)

import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;/** * @author skysnow * */public class myDateUtil {	/**	 *这里共有2个时间段(b1-----e1)【b2-----e2】,4个时间点;	 *相当于两条线段(b代表起点,e代表端点,b<=e),4个端点。	 *可分3种情况:	 *1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1
e2)此时,重合天数为零。 *2.相交。 *情况一:(b1---【b2---e1)----e2】 if(b1
b2) *情况二:【b2---(b1---e2】----e1) if(b1>b2&&b1
e2) *【b2---(b1-----e1)--e2】 if(b1>b2&&e1
=e2){//(b1---【b2-----e2】--e1) System.out.println("1包含2"); coincidenceday=getDayDifference(enddate2,begindate2); }else if(b1>=b2&&e1<=e2){//【b2---(b1-----e1)--e2】 System.out.println("2包含1"); coincidenceday=getDayDifference(enddate1,begindate1); }else if(b1>=b2&&b1<=e2&&e2<=e1){//【b2---(b1---e2】----e1) System.out.println("相交"); coincidenceday=getDayDifference(enddate2,begindate1); }else if(b1<=b2&&e1<=e2&&e1>=b2){//(b1---【b2---e1)----e2】 System.out.println("相交"); coincidenceday=getDayDifference(enddate1,begindate2); }else if(e1<=b2||b1>=e2){ coincidenceday="0"; }else{ coincidenceday=""; System.out.println("意料外的日期组合,无法计算重合天数!"); } System.out.println("重合天数为["+coincidenceday+"]天。"); return coincidenceday; } /** * 计算两个日期的相差天数(d1-d2) * @param d1 * @param d2 * @return */ public static String getDayDifference(Date d1,Date d2){ StringBuffer ds = new StringBuffer(); try{ long num = (d1.getTime()-d2.getTime())/1000; long days = num/(3600*24); if(days>=0)ds.append(days); }catch(Exception e){ ds=new StringBuffer(""); e.printStackTrace(); } return ds.toString(); } public static Date stringToDate(String strDate) { if (strDate==null){return null;} SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } public static String getThisMonth() { // 本月的第一天 Calendar calendar = new GregorianCalendar(); calendar.set(Calendar.DATE, 1); SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd"); String fd = simpleFormate.format(calendar.getTime()); // 本月的最后一天 calendar.set( Calendar.DATE, 1 ); calendar.roll(Calendar.DATE, - 1 ); String ld = simpleFormate.format(calendar.getTime()); return fd+","+ld; } public static void main(String[] args) { String[] thisMonth=getThisMonth().split(","); Date begindate1 = stringToDate(thisMonth[0]+" 00:05:00"); Date enddate1 = stringToDate(thisMonth[0]+" 24:05:00");; Date begindate2 = stringToDate(thisMonth[0]+" 00:05:00"); Date enddate2 = stringToDate(thisMonth[1]+" 00:00:00"); System.out.println(getDayCoincidence(begindate1, enddate1, begindate2, enddate2)); }}

转载地址:http://yjcmz.baihongyu.com/

你可能感兴趣的文章
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>