How to subtract dates ?

L

Lutek

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.
 
R

Ryan Stewart

Lutek said:
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.
 
J

Jon Caldwell

Ryan said:
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;
}
 
M

Michael Borgwardt

Jon said:
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.
 
J

Jacob

Lutek said:
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);
 
Joined
Apr 3, 2009
Messages
1
Reaction score
0
Lutek said:
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.


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

PHP:
  public static int subtractDays(Date date1, Date 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 = (GregorianCalendar) gc1.clone();
    for (int f = gctmp.get(Calendar.YEAR);  f < maxYear;  f++)
      {days1 += gctmp.getActualMaximum(Calendar.DAY_OF_YEAR);  gctmp.add(Calendar.YEAR, 1);}

    gctmp = (GregorianCalendar) gc2.clone();
    for (int f = gctmp.get(Calendar.YEAR);  f < maxYear;  f++)
      {days2 += gctmp.getActualMaximum(Calendar.DAY_OF_YEAR);  gctmp.add(Calendar.YEAR, 1);}

    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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top