Solved

Difference between 2 dates

Posted on 2004-04-15
6
148 Views
Last Modified: 2010-05-18
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
Comment
Question by:jrmc_97
6 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 63 total points
Comment Utility
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 62 total points
Comment Utility
>>Calendar c = Calendar.getInstance("UTC");

should be


Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Depends what your definition of the number of days between two dates is.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now