• 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

caculating the difference between dates

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the Comparing Dates section of our FAQ on Java Dates.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic