×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    33

    calculate date difference in days

    Is there a method to calculate de difference in days between two dates?

    When the first date is 1 June 23h and the second date is 2 June 1h it must return 1
    When the first date is 1 June 1h and the second date is 1 June 3h it must return 0

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: calculate date difference in days

    Try this...

    Code:
    private static final long DAY = 86400000;
    
    public static int daysBetween(Date first, Date second) {
        int days = (int) ((first.getTime() / DAY) - (second.getTime() / DAY));
        return days < 0 ? -days : days;
    }
    Graham.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    33

    Re: calculate date difference in days

    Quote Originally Posted by grahamhughes View Post
    Try this...

    Code:
    private static final long DAY = 86400000;
    
    public static int daysBetween(Date first, Date second) {
        int days = (int) ((first.getTime() / DAY) - (second.getTime() / DAY));
        return days < 0 ? -days : days;
    }
    Graham.
    isn't this the same like this: (simple math)
    int days = (int) ((first.getTime()-second.getTime()) / DAY);

    But in my first example the time between second and first is only 2 hours and this will after rounding result in zero.
    In both my two examples the difference in time between second and first is 2 hours, but the first is 1 day and the second is zero days difference.
    So I think I gonna need another principle than just look to the subtraction to determine the difference in days between both.

    I want to check with this function if a given date is today, tomorrow or the day after tomorrow or ...

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: calculate date difference in days

    No, it's not the same. Did you try it?

    Graham.

Similar Threads

  1. Applications uploaded 26 days ago, passed QE and still on visible on OVI store?
    By MTI500 in forum [Closed] Publishing to Nokia Store
    Replies: 2
    Last Post: 2009-09-14, 18:40
  2. How to calculate the time difference??
    By chandran.biju007 in forum Symbian
    Replies: 7
    Last Post: 2009-07-26, 04:20
  3. How to calculate date differences
    By t-kev in forum Symbian
    Replies: 7
    Last Post: 2009-05-29, 05:33
  4. New date: Java Mobile, Media & eMbedded Developer Days Conference
    By terrencebarr in forum Mobile Java General
    Replies: 0
    Last Post: 2008-09-25, 13:14
  5. Calendar adding 30 days
    By earamsey in forum Mobile Java General
    Replies: 4
    Last Post: 2006-12-22, 12:10

Posting Permissions

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