• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

Difference between 2 dates

I have seen most of the solutions found here.  But I am having trouble with the calulation of the number of days when using 04/01/2004 to 04/30/2004 is calculating 29 days instead of 30.  But when I use something like 04/01/2004 - 04/03/2004 it is calcualting 3 days which is correct.  Also it is correct when I use 4/5/2004 - 04/30/2004.  it appears to only give an incorrect number when I use a start date from standard time and a end date from daylight savings time.

Anyone else experience this?

I am using the difference in milliseconds, diffMilli/(24*60*60*1000).

thanks,

Jim
0
jrmc_97
Asked:
jrmc_97
2 Solutions
 
zzynxSoftware engineerCommented:
I think this will work (UTC doesn't use daylight saving stuff):

Calendar c = Calendar.getInstance("UTC");
c.set(2004, 4, 1);
long l1 = c.getTimeInMillis();
c.set(2004, 4, 3);
long l2 = c.getTimeInMillis();
long difference = l2-l1;
0
 
CEHJCommented:
>>Calendar c = Calendar.getInstance("UTC");

should be


Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
0
 
objectsCommented:
Depends what your definition of the number of days between two dates is.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I am using the difference in milliseconds, diffMilli/(24*60*60*1000).

Shouldn't do that. Java has all the API you need for dates.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now