Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to subtract dates ?

Reply
Thread Tools

How to subtract dates ?

 
 
Lutek
Guest
Posts: n/a
 
      01-04-2005
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.
 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      01-04-2005
"Lutek" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Jon Caldwell
Guest
Posts: n/a
 
      01-05-2005
Ryan Stewart wrote:
> "Lutek" <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      01-05-2005
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
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      01-05-2005
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.
 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      01-05-2005
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
 
Reply With Quote
 
pac0rro pac0rro is offline
Junior Member
Join Date: Apr 2009
Posts: 1
 
      04-03-2009
Quote:
Originally Posted by 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.

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

PHP Code:
  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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subtract dates with time module barronmo Python 3 04-02-2008 08:07 PM
Dates dates dates dates... SQL and ASP.NET David Lozzi ASP .Net 1 09-30-2005 02:18 PM
Dates! Dates! Dates! PW ASP General 4 08-09-2004 04:42 PM
newbie question on how to subtract two dates. Ruby Ruby Ruby 4 12-27-2003 06:45 AM
GregorianCalendar and how to subtract MONTH from a date. Manoj Nair Java 4 09-04-2003 05:49 AM



Advertisments