博客
关于我
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执行顺序与索引算法
查看>>
mysql批量update优化_Mysql中,21个写SQL的好习惯,你值得拥有呀
查看>>
mysql批量update操作时出现锁表
查看>>
MYSQL批量UPDATE的两种方式
查看>>
mysql批量修改字段名(列名)
查看>>
MySQL批量插入数据遇到错误1213的解决方法
查看>>
mysql技能梳理
查看>>
MySQL报Got an error reading communication packets错
查看>>
Mysql报错Can‘t create/write to file ‘/tmp/#sql_3a8_0.MYD‘ (Errcode: 28 - No space left on device)
查看>>
MySql报错Deadlock found when trying to get lock; try restarting transaction 的问题解决
查看>>
MySQL报错ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘
查看>>
Mysql报错Packet for query is too large问题解决
查看>>
mysql报错级别_更改MySQL日志错误级别记录非法登陆(Access denied)
查看>>
Mysql报错:too many connections
查看>>
MySQL报错:无法启动MySQL服务
查看>>
mysql授权用户,创建用户名密码,授权单个数据库,授权多个数据库
查看>>
mysql排序查询
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>