日期:2014-05-20 浏览次数:20770 次
public class Test { public static void main(String[] args) throws ParseException { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date d1Start = df.parse("2010-01-01"); Date d1End = df.parse("2011-01-01"); Date d2Start = df.parse("2010-06-01"); Date d2End = df.parse("2011-06-01"); System.out.println(calcOverlapDays(d1Start, d1End, d2Start, d2End)); } public static int calcOverlapDays(Date range1Start, Date range1End, Date range2Start, Date range2End) { long range1StartTime = range1Start.getTime(); long range1EndTime = range1End.getTime(); long range2StartTime = range2Start.getTime(); long range2EndTime = range2End.getTime(); assert range1StartTime > range1EndTime; assert range2StartTime > range2EndTime; long overlapTime = Math.min(range1EndTime, range2EndTime) - Math.max(range1StartTime, range2StartTime); return (overlapTime < 0) ? 0 : (int) (overlapTime / 24 / 60 / 60 / 1000 + 1); } }
------解决方案--------------------
不知道能不能满足你的要求!
//String time1_star = "2011-01-01"; String time1_end = "2011-05-01"; String time2_star = "2011-04-01"; //String time2_end="2011-06-01"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date time1 =sdf.parse(time1_end); Date time2 =sdf.parse(time2_star); long diffDay= (time1.getTime()-time2.getTime())/(3600*1000*24); if(diffDay > 0){ System.out.println("重合天数为:"+diffDay); }else{ System.out.println("没有重合天数"); } System.out.println(diffDay); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }