aspose file tools*
The moose likes Beginning Java and the fly likes java.sql.Date math Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.sql.Date math" Watch "java.sql.Date math" New topic
Author

java.sql.Date math

Patrick Mallahan
Ranch Hand

Joined: Apr 22, 2005
Posts: 69
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

Joined: Oct 26, 2004
Posts: 45
Use a java.util.Calendar or subclass thereof. It provides that functionality.


Christopher Elkins, SCJP Java 2 Platform
Patrick Mallahan
Ranch Hand

Joined: Apr 22, 2005
Posts: 69
what method in calender do you use? and where can i find thereof?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39571
    
  27
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


Ping & DNS - updated with new look and Ping home screen widget
Christopher Elkins
Ranch Hand

Joined: Oct 26, 2004
Posts: 45
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

Joined: May 22, 2003
Posts: 657

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

Joined: Apr 22, 2005
Posts: 69
thanks guys, i will try that
Jorge Blaz
Ranch Hand

Joined: Jan 31, 2004
Posts: 53
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

Joined: Apr 22, 2005
Posts: 69
wow, it works so good, and its not even difficult to set up, thanks all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.sql.Date math
 
Similar Threads
Converting From String to java.sql.Date
Variable type question?
java.sql.date to string
how to generate sql date
date problem