Results 1 to 6 of 6
  1. #1
    teo.danciu is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.
    Java 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);
        }
    }

  5. #5
    ruuebs is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    couldn't you just use some kind of function like...

    Java 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?

  6. #6
    teo.danciu is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default

    Hi,

    Thanks for your answers!
    I had problems with the division to 1000*60*60*24, because of the rounding & conversion from double to float...

    But I found a solution..

    Java Code:
     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;
        }
    However, I have to test it thoroughly .
    Thanks!

    Teo

Similar Threads

  1. Formatting time and date
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:35 PM
  2. Date/Time Servlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 09:34 AM
  3. File creating date/time
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-11-2007, 07:43 PM
  4. Time and Date in Java
    By java_fun2007 in forum New To Java
    Replies: 4
    Last Post: 11-06-2007, 07:25 PM
  5. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •