Database java date query

hello there,

i have this code below where i get todays data and want to query the databse to return all the values for today.but it does not return anything.
Timestamp  todayDate = null;
        String currentDate = null;
          SimpleDateFormat df = new SimpleDateFormat( "dd/MM/yy HH:mm:ss" ) ;
        java.util.Date d = new java.util.Date() ;
        try
        {
          currentDate = df.format( d ).toString();
          java.util.Date oDate1 = df.parse(currentDate);
                  long tf1 = oDate1.getTime();
                  todayDate = new Timestamp(tf1);
        }
        catch( Exception e )
        {
          System.out.println( e);
        }
        String dslamLinecard= "SELECT * FROM Bookht_User_Session_Action WHERE ACTION_TIME= " +todayDate;
zolfAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
try:

... where convert(VARCHAR(8), ACTION_TIME, 1) = convert(VARCHAR(8), GETDATE(), 1)
0
 
CEHJCommented:
If the field is a timstamp, you probably need to query *between* times or you will almost certainly get nothing
0
 
objectsCommented:
try something like:

SELECT * FROM Bookht_User_Session_Action WHERE DATE(ACTION_TIME)=?

exact format will depend on your database
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
zolfAuthor Commented:


ok then how do i get today's date and query the db to return all the rows with todays date.
please help
0
 
zolfAuthor Commented:

i am using sql server 2005.
objects:
'DATE' is not a recognized function name.
0
 
objectsCommented:
http://www.sql-server-helper.com/functions/get-date-only.aspx

you can also use GETDATE() in your query instead of having to build parameter.
Simpler and more efficient
0
 
zolfAuthor Commented:

i tried this but still nothing is returned
SELECT * FROM Bookht_User_Session_Action WHERE ACTION_TIME= getdate()
0
 
objectsCommented:
that because you still need to convert it to a date (as the times will not be the same as the current time)
0
 
objectsCommented:
another option would be to use convert to convert ACTION_TIME and GETDATE() to a date string and compare the strings

http://www.sql-server-helper.com/tips/date-formats.aspx
0
 
zolfAuthor Commented:

for some reason i cannot open these links which you provide.can you please give me the code example.appreciate your help.
i just need to get todays date and query db to return all the rows
0
 
CEHJCommented:
Not a good idea to use db-specific code. It will make your code unportable and there's no need for it anyway
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.