• 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

java.sql.Date math

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay I need to know if there is any way to find the difference in days between two java.sql.Date variables? It can also be java.util.Date as well because i convert it to that in my code.
[ July 12, 2005: Message edited by: Patrick Mallahan ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a java.util.Calendar or subclass thereof. It provides that functionality.
 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what method in calender do you use? and where can i find thereof?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can construct two Calendar objects using the fields from java.sql.Date, and then get their difference in milliseconds using getTimeInMillis(). From that it's easy to calculate the difference in days.
The API documentation is at http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
and http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html
 
Christopher Elkins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.GregorianCalendar is the only subclass (as of 1.4.2). I'd set two calendar objects using your two dates (java.util.Date):
GregorianCalendar c1 = new GregorianCalendar();
GregorianCalendar c2 = new GregorianCalendar();

c1.setTime(dateA);
c2.setTime(dateB);

You can then retrieve the day of the year from those calendar objects as an int:
int d1 = c1.get(Calendar.DAY_OF_YEAR);
int d2 = c2.get(Calendar.DAY_OF_YEAR);

Subtracting one from the other will give you the difference in days between the two dates:
int diff = d2 - d1;

This should work as long as the dates are within the same year. A little extra tweaking should make it work across years. Hope that helps.

java.util.Calendar
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calculating Java dates: Take the time to learn how to create and use dates
Working in Java time: Learn the basics of calculating elapsed time in Java

This is useful, too:
http://forum.java.sun.com/thread.jspa?threadID=488668
 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys, i will try that
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the Calendar object is a great way of handling dates. Some methods of the Date object have been deprecated in favor of using the ones provided by the Calendar object
 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow, it works so good, and its not even difficult to set up, thanks all.
reply
    Bookmark Topic Watch Topic
  • New Topic