This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes I want to compare 4 date values... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "I want to compare 4 date values..." Watch "I want to compare 4 date values..." New topic
Author

I want to compare 4 date values...

saran ram
Ranch Hand

Joined: Dec 13, 2002
Posts: 69
i have to compare 4 date values & find the greatest date in it..
plz reply me immediately if possible with some example code.
thanks in advance
Dave Turner
Ranch Hand

Joined: Mar 13, 2001
Posts: 60
Hi,
You can only compare two dates at a time, so you must compare the first two dates to determine which is greatest, then compare this greater date with the third date, again to find the greater value. Finally, repeat this process with the fourth date, to find the greatest value overall.
You can use the getTime() function of the java.util.Date class to obtain "the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object" as a long value, this will allow you to compare the dates easily.
Hope this helps,
Dave
[ July 07, 2003: Message edited by: Dave Turner ]
saran ram
Ranch Hand

Joined: Dec 13, 2002
Posts: 69
Thank you Dave,
I was successful in comparing 4 date values.
But I may get a requirement to compare more than 4 date values in near future.
so if there is someway to do this, reply me with some suggestions.
(i'm trying to use a hashtable or list & use collections methods, but yet to
suceed).
with regards,
Saran Ram
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Put the Dates in an array and sort the array using java.util.Arrays.sort(Object []). Dates implement Comperable, so you should get a sorted array of dates. The last one in the array is the most recent (greatest)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
 
subject: I want to compare 4 date values...
 
Similar Threads
Comparing two Dates
Sorting through Comparable(Very Urgent)
if tag in struts 2
DB2 query cannot work using preparedstatement
Struts 2 Multiple Input problem