Solved

Difference between 2 dates

Posted on 2004-04-15
6
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

718 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