Forums Register Login

comparing date fields from a database

+Pie Number of slices to send: Send
Please...please put me out of my misery!
WHY OH WHY IS WORKING WITH DATES SO HARD IN JAVA/JSP!
In ASP or ColdFusion comparing dates, getting the difference in years, months, weeks, days, hours, minutes, seconds is a breeze...
I have two dates in a database that I need to compare and get the difference in days...I can do it sitting on one hand.
I have to do this all the time in the apps I work on...there must be a better way to do this in JSP...
...please...please tell me how!
(begging for help...)
Byron
------------------

[This message has been edited by Byron Bignell (edited October 16, 2001).]
+Pie Number of slices to send: Send
Are you using the java.sql.Date and java.util.Date classes?
+Pie Number of slices to send: Send
 

Originally posted by Tim Holloway:
Are you using the java.sql.Date and java.util.Date classes?


hmmm...y'know, this nis the first I've heard of a sql.Date class...(guess that's why I'm here)
I was using the java.util.Date class and trying to get it to see the sql date as a date...no go...
this is what I'm doing...
rs.getDate("myDate")
and compare it to the current system date...
get the difference in days...
Byron
footnote:
Just looked at java.sql.Date...it's mostly deprecated...how useful can that be??

[This message has been edited by Byron Bignell (edited October 16, 2001).]
+Pie Number of slices to send: Send
Hey Byron:
Have you try java.util.Calendar? I think it is pretty neat. I can use this class for whatever I wanna use. Simple example for today date is:
Calendar today = Calendar.getInstance();
int month = today .get(Calendar.MONTH);
int year = today .get(Calendar.YEAR);
int today = today .get(Calendar.DAY_OF_MONTH);
Once you get and set two different date. You can compare as you wish. Cann't you? I hope this will help you. BK

Originally posted by Byron Bignell:
hmmm...y'know, this nis the first I've heard of a sql.Date class...(guess that's why I'm here)


+Pie Number of slices to send: Send
 

Originally posted by Bal Sharma:
Hey Byron:
Have you try java.util.Calendar? I think it is pretty neat. I can use this class for whatever I wanna use. Simple example for today date is:
Calendar today = Calendar.getInstance();
int month = today .get(Calendar.MONTH);
int year = today .get(Calendar.YEAR);
int today = today .get(Calendar.DAY_OF_MONTH);
Once you get and set two different date. You can compare as you wish. Cann't you? I hope this will help you. BK


Hmm...I'll give it a go.
thanks
byron
+Pie Number of slices to send: Send
I DID IT ! I DID I! (With a WHOLE LOT of help from people here!!)
This is what I did...
// Get the amount of time between the two dates
long msecs = date2.getTime() - date1.getTime();
// Create a GregorianCalendar and set its time zone to GMT
GregorianCalendar delta = new GregorianCalendar(
new SimpleTimeZone(5, ""));
// Set the calendar's time to the difference between the two
delta.setTime(new Date(Math.abs(msecs)));
// The "base" year is 1970, so subtract that number
out.println("Years: " +(delta.get(Calendar.YEAR) - 1970) + "<br>");
out.println("Months: " + delta.get(Calendar.MONTH) + "<br>");
// The "base" date is 1, so subtract that number
out.println("Days: " + (delta.get(Calendar.DATE) - 1) + "<br>");
out.println("Hours: " + delta.get(Calendar.HOUR) + "<br>");
out.println("Minutes: " + delta.get(Calendar.MINUTE) + "<br>");
out.println("Seconds: " + delta.get(Calendar.SECOND) + "<br>");
if(delta.get(Calendar.DATE) >= 3){
out.print("FLAGGED");
}
Question I have now is...can it be done more efficiently??
Byron
+Pie Number of slices to send: Send
 


Just looked at java.sql.Date...it's mostly deprecated...how useful can that be??


Actually, most of the deprecated date stuff has to do with removing the assumption that your date is given in respect to the Gregorian calendar. It may seem redundant to have 2 different date classes, but the java.sql.Date is more closely tied to SQL's database date type, whereas the java.util.Date works more closely with calendars and date manipulations. There's also the fact that the SQL date's resolution is much coarser (24 hours vs. milliseconds) - a limitation of the definition of an SQL date, not the java.sql.Date class.
[This message has been edited by Tim Holloway (edited October 17, 2001).]
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1844 times.
Similar Threads
How to calculate date from two fields
How to get the difference between 2 dates ?
FAQ - Everything you ever wanted to know about dates
comparison of dates
API methods for difference between two dates
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:10:57.