给定几对日期,如何计算中间的天数但不计算重叠天数?



这个问题在我的面试中被问到。

String d1="7 dec 2012";
String d2="15 dec 2012";
String d3="12 dec 2012";
String d4="16 dec 2012";
String d5="16 dec 2012";
String d6="24 dec 2012";
Number of days between d1 and d2 is: 9  
Number of days between d3 and d4 is: 1 (12 Dec to 15 Dec counted in d1 and d2,
                                        don't count overlapping days)  
Number of days between d3 and d4 is: 8 (16 Dec counted in d3 and d4, 
                                        don't count overlapping days)
Now final output should be: 9 + 1 + 8

我应该使用什么算法?

下面的代码是基本编码(非常简单的基本方法)计算2天的日期差。它没有考虑到夏令时和任何重叠的日期。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TestJava {
 public static int daysBetween(Date d1, Date d2)
 {
     /*This function returns the difference in days given 2 dates as input*/
         return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
 }
 public static void main(String args[]) throws ParseException
 {
     Calendar cal1 = new GregorianCalendar();
     Calendar cal2 = new GregorianCalendar();
     SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
     Date date = sdf.parse("07122012");  // Date1
     cal1.setTime(date);
     date = sdf.parse("15122012"); // Date2
     cal2.setTime(date);
     int daysBetween = daysBetween(cal1.getTime(),cal2.getTime());  // Days between Date1 and Date2
     int interval = daysBetween(cal1.getTime(),cal2.getTime());
     System.out.println("Interval1 == "+ interval);
     date = sdf.parse("12122012"); // Date3
     cal1.setTime(date);
     date = sdf.parse("16122012"); // Date4
     cal2.setTime(date);
     daysBetween += daysBetween(cal1.getTime(),cal2.getTime()); // Days between Date3 and Date4 is added to the previously available value of days between Date1 and Date2
     interval = daysBetween(cal1.getTime(),cal2.getTime());
     System.out.println("Interval2 == "+ interval);
     date = sdf.parse("16122012");
     cal1.setTime(date);
     date = sdf.parse("24122012");
     cal2.setTime(date);
     daysBetween += daysBetween(cal1.getTime(),cal2.getTime()); // Total required difference as per the question
     interval = daysBetween(cal1.getTime(),cal2.getTime());
     System.out.println("Interval3 == "+ interval);
     System.out.println("Total Interval == "+ daysBetween);
 }
}

这个问题应该分三步解决:

步骤1:查找重叠天数。

步骤2:找出给定日期之间的差异。

步骤3:用步骤2中得到的差值减去步骤1中得到的重叠天数,这将得到没有重叠日期的日期之间的确切差值.........!

下面的代码将满足您的目的:

package mypack;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatesNoOverlap 
{
public static void main(String args[])
{
    String d[]=new String[6];
    d[0]="7/12/2012";
    d[1]="15/12/2012";
    d[2]="12/12/2012";
    d[3]="16/12/2012";
    d[4]="16/12/2012";
    d[5]="24/12/2012";
    int num[]=new int[6];
    int counter=0;
    for(int i=0;i<d.length;i++)
    {
        for(int j=0;j<d[i].length();j++)
        {
            if(d[i].charAt(j)=='/')
            {
                num[counter]=Integer.parseInt(d[i].substring(0,j));
                counter++;
                break;
            }
        }
    }
    int sum1=0;
    for(int m=0;m<counter-1;m++)
    {
        if(m>0&&m%2!=0)
        {
            if(num[m+1]<=num[m])
            {
                sum1=sum1+(num[m]-num[m+1]+1);
            }
        }
    }
    System.out.println("Number of overlapping days........="+sum1);
    long sum2=0;
    for(int n=0;n<counter-1;n=n+2)
    {       
    SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
    Date d1 = null;
    Date d2 = null;
    try {
        d1 = format.parse(d[n]);
        d2 = format.parse(d[n+1]);
        //in milliseconds
        long diff = d2.getTime() - d1.getTime();
        long diffDays = diff / (24 * 60 * 60 * 1000);
                    sum2=sum2+diffDays+1;
    }
            catch (Exception e) {
        e.printStackTrace();
    }
    }
    System.out.println("Complete Difference = "+sum2);
    System.out.println("Exact Difference = "+(sum2-sum1));
}
}

最新更新