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

时间:2014-06-05 04:35:16

标签: java string date

我的采访中提到了这个问题。

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 个答案:

答案 0 :(得分:0)

以下代码是基本编码(非常简单的基本方法)来计算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 :(得分:0)

问题应分三步解决:

第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));
}

}