File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes comparing date fields from a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "comparing date fields from a database" Watch "comparing date fields from a database" New topic
Author

comparing date fields from a database

Byron Bignell
Greenhorn

Joined: Sep 29, 2001
Posts: 13
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).]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061
    
  21

Are you using the java.sql.Date and java.util.Date classes?


Customer surveys are for companies who didn't pay proper attention to begin with.
Byron Bignell
Greenhorn

Joined: Sep 29, 2001
Posts: 13
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).]
Bal Sharma
Ranch Hand

Joined: Sep 19, 2001
Posts: 273
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)

Byron Bignell
Greenhorn

Joined: Sep 29, 2001
Posts: 13
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
Byron Bignell
Greenhorn

Joined: Sep 29, 2001
Posts: 13
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
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061
    
  21


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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing date fields from a database