Discussion:
Getting Date Difference in Java.
(too old to reply)
Lucky
2005-05-06 13:55:44 UTC
Permalink
Hi
In oracle, if I need to get the sysdate, I would do some thing like this
select sysdate from dual;
And if I need , the difference between sysdate and a specific date ,say
"pdate" ,I would do,
select sysdate - pdate from dual
This would give me the difference in days ,so for ex
select sysdate -to_date('01-JAN-2005') from dual
would give me 125 as the number of days.
---
Now what I want to know is that in java how do I this. I tried to play
around with the Date and Calendar objects.
These objects are giving me individual feilds like day,month and year etc,
but not date as a whole. So my question is

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.
2. How do I get the difference in 2 dates, which should give me in number of
days.

Your quick responses are appreciated.

Thanks,

PS. By the way how do we do the same in JavaScript.
kaeli
2005-05-06 16:19:21 UTC
Permalink
In article <DdGdndOEy_QM6-bfRVn-***@comcast.com>, ***@comcast.net
enlightened us with...
Post by Lucky
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
Post by Lucky
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.
--
--
~kaeli~
Cthulhu Saves. He might get hungry later.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
Wannabee
2005-05-09 12:46:22 UTC
Permalink
"kaeli"
Post by kaeli
enlightened us with...
Post by Lucky
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.
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 ...
Josef Garvi
2005-05-09 15:16:29 UTC
Permalink
Post by Wannabee
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
Wannabee
2005-05-10 06:43:19 UTC
Permalink
"Josef Garvi"
Post by Josef Garvi
Post by Wannabee
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
Post by Josef Garvi
Post by Wannabee
(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 ...
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.
Usenet Reader
2005-06-26 01:15:35 UTC
Permalink
Post by Wannabee
"Josef Garvi"
Post by Josef Garvi
Post by Wannabee
java.util.Calendar.getTimeInMillis() and java.util.Date.getTime() give
the
Post by Josef Garvi
Post by Wannabee
number of milliseconds (from 1.1.1970). Maybe you could use them? There
is
Post by Josef Garvi
Post by Wannabee
24 * 60 * 60 * 1000 milliseconds in a day.
Eg. if the hours, minutes and seconds are the same in two
Calendar -objects
Post by Josef Garvi
Post by Wannabee
(eg. all zero) then you could convert them to milliseconds using the
above
Post by Josef Garvi
Post by Wannabee
method, take the difference between the millisecond -values, divide it
by
Post by Josef Garvi
Post by Wannabee
86400000 and get the difference in days? Just an idea ...
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
- 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.
Roedy Green
2005-06-26 01:32:12 UTC
Permalink
On Sun, 26 Jun 2005 01:15:35 GMT, Usenet Reader
Post by Wannabee
I think the OP wanted the integral number of days between two dates
See http://mindprod.com/jgloss/calendar.html
for how to do date/time voodoo.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
IchBin
2005-06-26 01:47:54 UTC
Permalink
Post by Roedy Green
On Sun, 26 Jun 2005 01:15:35 GMT, Usenet Reader
Post by Wannabee
I think the OP wanted the integral number of days between two dates
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-)
Roedy Green
2005-06-26 07:56:10 UTC
Permalink
Post by IchBin
Hi Roedy,
You do not know me but it nice to see you back in the newsgroups..
I'm not really back. I'm buried to my eyeballs in cleaning up my own
website.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
EricF
2005-05-07 04:18:34 UTC
Permalink
Post by Lucky
Hi
In oracle, if I need to get the sysdate, I would do some thing like this
select sysdate from dual;
And if I need , the difference between sysdate and a specific date ,say
"pdate" ,I would do,
select sysdate - pdate from dual
This would give me the difference in days ,so for ex
select sysdate -to_date('01-JAN-2005') from dual
would give me 125 as the number of days.
---
Now what I want to know is that in java how do I this. I tried to play
around with the Date and Calendar objects.
These objects are giving me individual feilds like day,month and year etc,
but not date as a whole. So my question is
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.
SimpleDateFormat
Post by Lucky
2. How do I get the difference in 2 dates, which should give me in number of
days.
Take a look at the GregorianCalendar class
Post by Lucky
Your quick responses are appreciated.
Thanks,
PS. By the way how do we do the same in JavaScript.
Loading...