Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Difference between 2 dates

Posted on 2004-04-15
6
Medium Priority
?
156 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 252 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 248 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

636 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