Solved

Difference between 2 dates

Posted on 2004-04-15
6
150 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
ID: 10836070
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
ID: 10836159
>>Calendar c = Calendar.getInstance("UTC");

should be


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

Expert Comment

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

Expert Comment

by:Mayank S
ID: 10839446
>> 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

803 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