博客
关于我
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/

你可能感兴趣的文章
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>