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

你可能感兴趣的文章
MySQL索引下沉:提升查询性能的隐藏秘
查看>>
MySql索引为什么使用B+树
查看>>
MySQL索引为什么是B+树
查看>>
WARNING!VisualDDK wizard was unable to find any DDK/WDK installed on your system.
查看>>
MySQL索引介绍及百万数据SQL优化实践总结
查看>>
Mysql索引优化
查看>>
MySQl索引创建
查看>>
mysql索引创建及使用注意事项
查看>>
mysql索引创建和使用注意事项
查看>>
MySQL索引原理以及查询优化
查看>>
Mysql索引合并(index merge)导致的死锁问题
查看>>
MySQL索引和查询优化
查看>>
mysql索引底层数据结构和算法
查看>>
Mysql索引底层结构的分析
查看>>
MySQL索引底层:B+树详解
查看>>
Mysql索引总结
查看>>
mysql索引最左匹配原则理解以及常见的sql使用的索引情况的实测
查看>>
Mysql索引类型
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
mysql索引能重复吗_mysql “索引”能重复吗?“唯一索引”与“索引”区别是什么?...
查看>>