This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

caculating the difference between dates  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good morning everyone i have the following code which accesses as database of customer reccords, i need some help in caculating the difference in days between dates and then display the answer under the database. for example the difference between 11/06/2004 and 14/6/2004. iam thinking that it would be some type of IF statement but i have never been good at that so any help would be greatly appreciated... also if someone could tell me where to actually put the code that would also be alot of help...
there are two classes for this
---------------------------------------------------------------------------
CLASS ONE
import java.sql.*;
import java.util.*;
import javax.swing.table.*;


public class ResultSetTableModel extends AbstractTableModel {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;

public ResultSetTableModel(String query) throws SQLException, ClassNotFoundException
{
String url = "jdbc dbc:customers";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

connection = DriverManager.getConnection(url);

statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);

setQuery(query);

}

public Class getColumnClass(int column)
{
try{
String className =
metaData.getColumnClassName(column + 1);

return Class.forName(className);
}
catch(Exception exception){
exception.printStackTrace();
}

return Object.class;

}

public int getColumnCount()
{

try{
return metaData.getColumnCount();
}

catch(SQLException sqlException){
sqlException.printStackTrace();
}

return 0;
}

public String getColumnName(int column)
{
try{
return metaData.getColumnName(column + 1);
}

catch (SQLException sqlException){
sqlException.printStackTrace();
}

return "";
}

public int getRowCount()
{
return numberOfRows;
}

public Object getValueAt(int row, int column)
{

try{
resultSet.absolute(row + 1);
return resultSet.getObject(column + 1);
}

catch ( SQLException sqlException){
sqlException.printStackTrace();
}

return "";
}

protected void finalize()
{

try{
statement.close();
connection.close();
}

catch (SQLException sqlException){
sqlException.printStackTrace();
}
}

public void setQuery (String query) throws SQLException
{
resultSet = statement.executeQuery(query);

metaData = resultSet.getMetaData();

resultSet.last();
numberOfRows = resultSet.getRow();

fireTableStructureChanged();
}
}
---------------------------------------------------------------------------
CLASS TWO
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class DisplayQueryResults extends JFrame {
private ResultSetTableModel tableModel;
private JTextArea queryArea;

public DisplayQueryResults()
{

super("Displaying Query results");

String url = "jdbc.odbc:customers";

String query = "SELECT * FROM custDetails";

try{
tableModel = new ResultSetTableModel(query);

queryArea = new JTextArea(query,3,100);
queryArea.setWrapStyleWord(true);
queryArea.setLineWrap(true);

JScrollPane scrollPane = new JScrollPane(queryArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

JButton submitButton = new JButton("Submit Query");

Box box = Box.createHorizontalBox();
box.add(scrollPane);
box.add(submitButton);

JTable resultTable = new JTable(tableModel);

Container c = getContentPane();
c.add(box, BorderLayout.NORTH);
c.add(new JScrollPane(resultTable),
BorderLayout.CENTER);

submitButton.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent e)
{
try{
tableModel.setQuery(queryArea.getText());
}

catch (SQLException sqlException){
JOptionPane.showMessageDialog(null,
sqlException.toString(),
"Database error",
JOptionPane.ERROR_MESSAGE);
}
}
}
);

setSize(500,250);
setVisible(true);
}

catch (ClassNotFoundException classNotFound){
JOptionPane.showMessageDialog(null,
"Cloudscape driver not found","Driver not found",
JOptionPane.ERROR_MESSAGE);

System.exit(1);
}

catch (SQLException sqlException){
JOptionPane.showMessageDialog(null,
sqlException.toString(),
"Database error",JOptionPane.ERROR_MESSAGE);

System.exit(1);
}
}

public static void main(String args[])
{
DisplayQueryResults app = new DisplayQueryResults();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at my post just a couple posts below this one regarding the date calculation.

I'm not sure how calculating the number of days between 2 dates fits into either one of the classes you posted though? Maybe if you explain how you are trying to use it a little better...

Also, when you post code, use the UBB code tags in order to maintain formatting and enhance readability. And try to post only as much code as needed to explain/demonstrate your problem.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had this problem recently.

The only way to do this is by using Calendar objects to hold the times, then setting the times for each day to 00:00:00.0000, and to count the difference in milliseconds between the two dates, then dividing by number-of-millis-perday.

That will give you the difference in number of days where eg:
"1st Jan 2004 23:00:00" compared to "2nd Jan 00:00:00" needs to be considered 1 days difference.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the Comparing Dates section of our FAQ on Java Dates.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!