• 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

problem with java.util.Date()

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, here is one problem I am facing while using java.util.Date() class.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(new java.util.Date());
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
sd.format(new java.util.Date());

Statement stmt = Conn.createStatement(); // Conn object is already created
String query = "select sum(ctotal) from creg where date_time between '"+sd.format(new java.util.Date())+"' and '"+sdf.format(new java.util.Date())+"'";
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
{
totalCash = rs.getFloat(1);
}

Here in this code, I'm not getting the proper value for "new java.util.Date()" when using JSP with Tomcat5.5 (jakarta-tomcat-5.5.9)??
I cant trace what's the exact problem.
All suggestion are welcome !
Thanks.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nikhil,

You should use a PreparedStatement rather than a Statement to insulate your code from the format the database wants a Date in. Then your query looks like:
select sum(ctotal) from creg where date_time between ? and ?

and you can call:
java.util.Date now = new java.util.Date();
preparedStatement.setDate(1, new java.sql.Date(now.getTime()));
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to move this to our JDBC form since it is about data access.

And welcome to JavaRanch!
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
Nikhil,

You should use a PreparedStatement rather than a Statement to insulate your code from the format the database wants a Date in. Then your query looks like:
select sum(ctotal) from creg where date_time between ? and ?

and you can call:
java.util.Date now = new java.util.Date();
preparedStatement.setDate(1, new java.sql.Date(now.getTime()));



And if you also want to compare on the date AND time, then you need to use java.sql.Timestamp instead of java.sql.Date in the preparedStatement.

java.util.Date now = new java.util.Date();
preparedStatement.setTimestamp(1, new java.sql.Timestamp(now.getTime()));
[ August 30, 2008: Message edited by: Rene Larsen ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic