# Time-Date problem

• 08-26-2008, 04:20 PM
teo.danciu
Time-Date problem
Hi,

I have the following problem, which I have been trying to solve for some days now:

I need to compute the number of days between two dates. However, there are two problems:
1) it's not the number of WHOLE days what I need, but the "ceiled" number of days (for instance, for 3 days and 4 hours, I need to return 4 days).
2) both margins of the interval must be included in the count, and not only one, as usually.

For instance, let the two dates be:

25 Aug 2008 and
30 Aug 2008

Then, the logic I want to implement is as follows:

25 - 1 day
26 - 1 day
27 - 1 day
28 - 1 day
29 - 1 day
30 - 1 day,

regardless of the hour of this dates..

I have tried various implementations, including the joda-time library, and other algorithms I have found on the web. However, none seems to be appropriate for this problem.
I have the feeling that the issue of the time of the day is the one which messes the computations up...

Do you have any ideas on how to solve this problem?

Thanks,

Teo
• 08-26-2008, 06:06 PM
Norm
You don't show what code you've tried and how it was wrong.
For the example you show: Aug 25 to Aug 30 is the answer to your question for these two dates 6 days?
In simple arithmetic to get that value, 30-25+1 would give that answer.
• 08-26-2008, 06:39 PM
Eranga
How many times senior members advice regarding this. Please ask questions specific to what you have done. Basically with the code where you stuck with. Please.
• 08-26-2008, 09:38 PM
hardwired
I have the feeling that the issue of the time of the day is the one which messes the computations up...
Then get rid of the time of day variance in the dates.
Code:

```import java.text.DateFormat; import java.util.*; public class Test {     static DateFormat df =         DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);     static Random seed = new Random();     public static void main(String[] args) {         Calendar begin = Calendar.getInstance();         begin.setTime(new Date());         System.out.println("begin = " + df.format(begin.getTime()));         System.out.println("------------------");         Calendar end = Calendar.getInstance();         for(int i = 0; i < 5; i++) {             end.setTime(getDate(begin.get(Calendar.YEAR)));             System.out.printf("end = %s%n", df.format(end.getTime()));             long days = getDays(begin.getTime(), end.getTime());             System.out.println("days = " + days);         }     }     /** Make up a random Date in this year. */     private static Date getDate(int year) {         Calendar calendar = Calendar.getInstance();         calendar.set(Calendar.YEAR, year);         int month = seed.nextInt(12);         calendar.set(Calendar.MONTH, month);         int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);         int day = seed.nextInt(maxDays);         calendar.set(Calendar.DAY_OF_MONTH, day);         calendar.set(Calendar.HOUR_OF_DAY, seed.nextInt(24+1));         return calendar.getTime();     }     /** Calculate the number of days in between. */     private static long getDays(Date start, Date end) {         Calendar c1 = Calendar.getInstance();         c1.setTime(start);         normalize(c1);         Calendar c2 = Calendar.getInstance();         c2.setTime(end);         normalize(c2);         System.out.printf("c1 = %s  c2 = %s%n",                 df.format(c1.getTime()), df.format(c2.getTime()));         long difference = c2.getTimeInMillis() - c1.getTimeInMillis();         long millisPerDay = 24*60*60*1000;         long days = difference/millisPerDay;         return days;     }     /** Set HOUR and MINUTE fields to noon. */     private static void normalize(Calendar calendar) {         calendar.set(Calendar.HOUR_OF_DAY, 12);         calendar.set(Calendar.MINUTE, 0);         calendar.set(Calendar.SECOND, 0);     } }```
• 08-26-2008, 09:54 PM
ruuebs
couldn't you just use some kind of function like...

Code:

```  public long DateDiff(Date start, Date end){                 GregorianCalendar startDate = new GregorianCalendar();         GregorianCalendar endDate = new GregorianCalendar();                 startDate.setTime(start);         endDate.setTime(end);         //Get the difference and divide by Milliseconds in a day         //then add initial start day.         return (endDate.getTimeInMillis() - startDate.getTimeInMillis())         / (24L * 60L * 60L * 1000L) + 1;               }```
or something like that?
• 08-27-2008, 10:01 AM
teo.danciu
Hi,

``` public long getCeiledDurationInDays(long startDate, long endDate) {         Calendar start = Calendar.getInstance();         start.setTimeInMillis(startDate);         Calendar end = Calendar.getInstance();         end.setTimeInMillis(endDate);         //set the end date to be the latest time in that day         end.set(Calendar.HOUR_OF_DAY, 23);         end.set(Calendar.AM_PM, Calendar.PM);         end.set(Calendar.MINUTE, 59);         end.set(Calendar.MILLISECOND, 99);         //this is from joda-time library, it computes the number         //of whole days within the interval         int  daysBetween = Days.daysIn(new org.joda.time.Interval(startDate, endDate)).getDays();         start.add(Calendar.DAY_OF_MONTH, daysBetween);         if (start.before(end)) {             daysBetween++;         }         return daysBetween;     }```