How to subtract dates ?

Discussion in 'Java' started by Lutek, Jan 4, 2005.

  1. Lutek

    Lutek Guest

    Date date_one;
    Date date_two;
    Dates have this format 2004.10.12

    How to subtract it and get result in days.

    Or


    Calendar date_1 = Calendar.getInstance();
    Calendar date_2 = Calendar.getInstance();

    How to subtract it and get result in days.
     
    Lutek, Jan 4, 2005
    #1
    1. Advertising

  2. Lutek

    Ryan Stewart Guest

    "Lutek" <> wrote in message
    news:Xns95D4E9CF88ACLutekID1@193.110.122.97...
    > Date date_one;
    > Date date_two;
    > Dates have this format 2004.10.12
    >
    > How to subtract it and get result in days.
    >
    > Or
    >
    >
    > Calendar date_1 = Calendar.getInstance();
    > Calendar date_2 = Calendar.getInstance();
    >
    > How to subtract it and get result in days.


    Perhaps the most simplistic way: Date has a getTime method and Calendar has
    a getTimeInMillis method. Both return a long representing the date in
    milliseconds. Subtract one from the other, do some division, and you have
    days of difference.
     
    Ryan Stewart, Jan 4, 2005
    #2
    1. Advertising

  3. Lutek

    Jon Caldwell Guest

    Ryan Stewart wrote:
    > "Lutek" <> wrote in message
    > news:Xns95D4E9CF88ACLutekID1@193.110.122.97...
    >
    >>Date date_one;
    >>Date date_two;
    >>Dates have this format 2004.10.12
    >>
    >>How to subtract it and get result in days.
    >>
    >>Or
    >>
    >>
    >>Calendar date_1 = Calendar.getInstance();
    >>Calendar date_2 = Calendar.getInstance();
    >>
    >>How to subtract it and get result in days.

    >
    >
    > Perhaps the most simplistic way: Date has a getTime method and Calendar has
    > a getTimeInMillis method. Both return a long representing the date in
    > milliseconds. Subtract one from the other, do some division, and you have
    > days of difference.
    >
    >

    This is flawed during daylight savings.

    Here is what I have done to solve the problem:
    public static int getDaysDiff(Date date1, Date date2)
    {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date1);

    int d1 = cal.get(Calendar.DATE);

    cal.setTime(date2);

    int d2 = cal.get(Calendar.DATE);

    return d2 - d1;
    }
     
    Jon Caldwell, Jan 5, 2005
    #3
  4. Lutek

    P.Hill Guest

    Lutek wrote:
    > Date date_one;
    > Date date_two;
    > Dates have this format 2004.10.12
    >
    > How to subtract it and get result in days.


    You can use a SimpleDateFormat to parse that into
    a Date then push each into a Calendar and then
    use some code like the following link to do the right
    thing.

    http://www.xmission.com/~goodhill/dates/deltaDates.html

    -Paul
     
    P.Hill, Jan 5, 2005
    #4
  5. Jon Caldwell wrote:
    > Here is what I have done to solve the problem:
    > public static int getDaysDiff(Date date1, Date date2)
    > {
    > Calendar cal = Calendar.getInstance();
    > cal.setTime(date1);
    >
    > int d1 = cal.get(Calendar.DATE);
    >
    > cal.setTime(date2);
    >
    > int d2 = cal.get(Calendar.DATE);
    >
    > return d2 - d1;
    > }


    Which works only if both dates are within the same calendar month,
    i.e. rarely.
     
    Michael Borgwardt, Jan 5, 2005
    #5
  6. Lutek

    Jacob Guest

    Lutek wrote:
    > Date date_one;
    > Date date_two;
    > Dates have this format 2004.10.12
    >
    > How to subtract it and get result in days.
    >
    > Or
    >
    >
    > Calendar date_1 = Calendar.getInstance();
    > Calendar date_2 = Calendar.getInstance();
    >
    > How to subtract it and get result in days.


    Use the Day class (http://geosoft.no/software) to get
    rid of the nasty time component which causes all sorts
    of problems:

    Day day1 = new Day (date_one);
    Day day2 = new Day (date_two);
    int nDays = day1.daysBetween (day2);

    To populate day_one and day_two in the first place, do:

    DateFormat dateFormat = new SimpleDateFormat ("yyyy.MM.dd");
    Date date_one = dateFormat.parse ("2004.10.12"); // try-catch omitted
    Date date_two = dateFormat.parse (whatever);


    --
    Jacob Dreyer
    G - Free 2D Graphics Library for Java - http://geosoft.no/graphics
    Free Java Software - http://geosoft.no/software
     
    Jacob, Jan 5, 2005
    #6
  7. Lutek

    pac0rro

    Joined:
    Apr 3, 2009
    Messages:
    1

    For me the following static method works (avoid DST problems):

    PHP:
      public static int subtractDays(Date date1Date date2)
      {
        
    GregorianCalendar gc1 = new GregorianCalendar();  gc1.setTime(date1);
        
    GregorianCalendar gc2 = new GregorianCalendar();  gc2.setTime(date2);

        
    int days1 0;
        
    int days2 0;
        
    int maxYear Math.max(gc1.get(Calendar.YEAR), gc2.get(Calendar.YEAR));

        
    GregorianCalendar gctmp = (GregorianCalendargc1.clone();
        for (
    int f gctmp.get(Calendar.YEAR);  maxYear;  f++)
          {
    days1 += gctmp.getActualMaximum(Calendar.DAY_OF_YEAR);  gctmp.add(Calendar.YEAR1);}

        
    gctmp = (GregorianCalendargc2.clone();
        for (
    int f gctmp.get(Calendar.YEAR);  maxYear;  f++)
          {
    days2 += gctmp.getActualMaximum(Calendar.DAY_OF_YEAR);  gctmp.add(Calendar.YEAR1);}

        
    days1 += gc1.get(Calendar.DAY_OF_YEAR) - 1;
        
    days2 += gc2.get(Calendar.DAY_OF_YEAR) - 1;

        return (
    days1 days2);
      }
    Note that this method can return negative values (useful to compare dates at the same time). You can call Math.abs( subtractDays(...)) to return positive integers.
     
    pac0rro, Apr 3, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    868
    Rob Schieber
    Sep 30, 2005
  2. Manoj Nair
    Replies:
    4
    Views:
    21,354
    Manoj Nair
    Sep 4, 2003
  3. barronmo

    subtract dates with time module

    barronmo, Mar 26, 2008, in forum: Python
    Replies:
    3
    Views:
    1,047
    barronmo
    Apr 2, 2008
  4. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    198
    Mark Schupp
    Aug 9, 2004
  5. Ruby Ruby
    Replies:
    4
    Views:
    218
    Osuka Adartse
    Dec 27, 2003
Loading...

Share This Page