Re: Getting Date Difference in Java.

K

kaeli

1. How do I get the date in java in a specific format (in any format I
choose). What is the best object for this.
java.text.SimpleDateFormat

2. How do I get the difference in 2 dates, which should give me in number of
days.

Calendar.add()
Use negative numbers to subtract.

--
 
W

Wannabee

"kaeli"
Calendar.add()
Use negative numbers to subtract.

java.util.Calendar.getTimeInMillis() and java.util.Date.getTime() give the
number of milliseconds (from 1.1.1970). Maybe you could use them? There is
24 * 60 * 60 * 1000 milliseconds in a day.

Eg. if the hours, minutes and seconds are the same in two Calendar -objects
(eg. all zero) then you could convert them to milliseconds using the above
method, take the difference between the millisecond -values, divide it by
86400000 and get the difference in days? Just an idea ...
 
J

Josef Garvi

Wannabee said:
java.util.Calendar.getTimeInMillis() and java.util.Date.getTime() give the
number of milliseconds (from 1.1.1970). Maybe you could use them? There is
24 * 60 * 60 * 1000 milliseconds in a day.

Eg. if the hours, minutes and seconds are the same in two Calendar -objects
(eg. all zero) then you could convert them to milliseconds using the above
method, take the difference between the millisecond -values, divide it by
86400000 and get the difference in days? Just an idea ...

Not completely accurate, as there are "leap seconds" now and then:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
W

Wannabee

"Josef Garvi"
Not completely accurate, as there are "leap seconds" now and then:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html

I think the OP wanted the integral number of days between two dates. Leap
seconds have no role in that task. That could be calculated thus:
- Make sure to use long numbers (not int) because these are large integers.
- Get the 2 dates into two java.util.Calendar -objects (or
GregorianCalendar)
- Convert them to milliseconds using the method long
Calendar.getTimeInMillis()
- Take the difference (long) between the millisecond -values by subtracting
one from the other.
- In case the answer is negative you might want to change it positive.
- Divide the difference in milliseconds by 86400000 (which is 24 * 60 * 60 *
1000, number of milliseconds in 1 day).
- Make sure to use integer division, it's exactly what you want. You don't
need the fractional part. You don't want to round the result to the nearest
integer, you want to "cut the number" and that's what integer division does.
You want integer divison between two long values.
- You get the integral number of days between those dates.
 
U

Usenet Reader

Wannabee said:
"Josef Garvi"


Calendar -objects



I think the OP wanted the integral number of days between two dates. Leap
seconds have no role in that task. That could be calculated thus:
- Make sure to use long numbers (not int) because these are large integers.
- Get the 2 dates into two java.util.Calendar -objects (or
GregorianCalendar)
- Convert them to milliseconds using the method long
Calendar.getTimeInMillis()
- Take the difference (long) between the millisecond -values by subtracting
one from the other.
- In case the answer is negative you might want to change it positive.
- Divide the difference in milliseconds by 86400000 (which is 24 * 60 * 60 *
1000, number of milliseconds in 1 day).
- Make sure to use integer division, it's exactly what you want. You don't
need the fractional part. You don't want to round the result to the nearest
integer, you want to "cut the number" and that's what integer division does.
You want integer divison between two long values.
- You get the integral number of days between those dates.

Almost, but leap seconds WILL affect the result (certainly, if one time
stamp happens to be within the leap second - you could be a day off in
your calculation).

After you come up with your result (as in the above), you can double
check it by adding it back to the lessor of the two Calendar objects
with the available methods, and make sure the result matches the greater
of the two. Under all but unreasonable circumstances, you won't have to
make more than an adjustment of one day.
 
I

IchBin

Roedy said:
See http://mindprod.com/jgloss/calendar.html
for how to do date/time voodoo.

Hi Roedy,

You do not know me but it nice to see you back in the newsgroups...

--


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
__________________________________________________________________________

' If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
R

Roedy Green

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,298
Latest member
ZenLeafCBDSupplement

Latest Threads

Top