Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.sql.Date math

 
Patrick Mallahan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • 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 ]
 
Christopher Elkins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
what method in calender do you use? and where can i find thereof?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks guys, i will try that
 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
wow, it works so good, and its not even difficult to set up, thanks all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic