博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java计算两个时间段的重合天数
阅读量:660 次
发布时间: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/

你可能感兴趣的文章