Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes how to find the difference between Dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to find the difference between Dates" Watch "how to find the difference between Dates" New topic
Author

how to find the difference between Dates

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi All,

i have to allow the user into Database table ,who is having minimum 1year service in a company..

for that i have to find today date and user's date of join from database..

and then subtract..right? but..i confuse..how to do it?..how to find the difference between two days?

Thanks & Regards,
seetharaman.v
Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 166
Hi
Why don't you handle this in your query itself?
Sysdate will give you current date and time, and the date of joining that would have been stored in the table can be subtracted. If its stored as Varchar(instead of date) use the inbuilt to_date function.
This, alongwith a bit of googling will get you through.

Jhakda


If I become filthy rich, I'll sponsor research for painless dental treatment at Harvard Medical School. Thats why,I'm learning Java.I have 32 teeth, 22 are man made.
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Check the methods from java.util.Calendar
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
check compareTo() method


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Alan Christen
Greenhorn

Joined: Sep 18, 2006
Posts: 16
The Google query

http://www.google.com/search?hl=en&q=how+to+find+the+difference+between+Dates+%2Bjava&btnG=Google+Search

returns 242,000 hits.

In this *huge* result set, I found this url:

http://www.exampledepot.com/egs/java.util/CompDates.html

Time spent: 3 minutes.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Originally posted by Alan Christen:
The Google query

http://www.google.com/search?hl=en&q=how+to+find+the+difference+between+Dates+%2Bjava&btnG=Google+Search

returns 242,000 hits.

In this *huge* result set, I found this url:

http://www.exampledepot.com/egs/java.util/CompDates.html

Time spent: 3 minutes.

There is one problem with that code: it is defective because of daylight savings time. Because of that, days can have 23 or 25 hours as well.


Using Calendar.get(Calendar.DAY_OF_YEAR) and Calendar.get(Calendar.YEAR), combined with Calendar.getActualMaximum(Calendar.DAY_OF_YEAR), you can calculate the result more precisely. Search on this forum, I've answered this question at least 3 times already.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

thanks All
 
Consider Paul's rocket mass heater.
 
subject: how to find the difference between Dates