• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparing date fields from a database

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using the java.sql.Date and java.util.Date classes?
 
Byron Bignell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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).]
reply
    Bookmark Topic Watch Topic
  • New Topic