Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

Getting Date Difference in Java.

Thread view: 
Lucky - 06 May 2005 14:52 GMT
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 - 06 May 2005 17:19 GMT
> 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.

Signature

--
~kaeli~
Cthulhu Saves. He might get hungry later.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Wannabee - 09 May 2005 13:46 GMT
"kaeli"

> > 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 - 09 May 2005 16:15 GMT
> 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
[quoted text clipped - 4 lines]
> 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

Signature

Josef Garvi

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

new income - better environment - more food - less poverty

Wannabee - 10 May 2005 07:43 GMT
"Josef Garvi"

> > 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
[quoted text clipped - 8 lines]
>
> 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 - 26 Jun 2005 02:15 GMT
> "Josef Garvi"
>
[quoted text clipped - 43 lines]
> 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 - 26 Jun 2005 02:32 GMT
>> 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.

Signature

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 - 26 Jun 2005 02:47 GMT
>>> 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...

Signature

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 - 26 Jun 2005 08:56 GMT
>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.

Signature

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

John McGrath - 06 May 2005 20:21 GMT
> Newsgroups: comp.lang.java.help
> Followup-To: alt.comp.lang.java, alt.comp.lang.javascript,
> comp.lang.java.databases, comp.lang.java.databses,
> comp.lang.java.developer, comp.lang.java.help, comp.lang.java.programmer

Why did you set follow-ups to all of these different groups?

Signature

Regards,

John McGrath

Thomas G. Marshall - 08 May 2005 02:45 GMT
John McGrath coughed up:

>> Newsgroups: comp.lang.java.help
>> Followup-To: alt.comp.lang.java, alt.comp.lang.javascript,
[quoted text clipped - 3 lines]
>
> Why did you set follow-ups to all of these different groups?

Especially since:

1. comp.lang.java.developer is not an official group.
2. comp.lang.java.databses is a misspelling.
3. alt.comp.lang.java and alt.comp.lang.javascript I've never even heart of
before.

Signature

I've seen this a few times--Don't make this mistake:

Dwight: "This thing is wildly available."
Smedly: "Did you mean wildly, or /widely/ ?"
Dwight: "Both!", said while nodding emphatically.

Dwight was exposed to have made a grammatical
error and tries to cover it up by thinking
fast.  This is so painfully obvious that he
only succeeds in looking worse.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.