• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.sql.Timestamp question

 
San Smith
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is the first time I am working with java.sql.Timestamp. I need to pass two values one is Start time and end time in my SQL SELECT query.

Start time will be today date and time is 12AM
End time will be yesterday’s date and time is 12AM


Example: Today’s date in Timestamp format is: 2011-08-22 09:42:37.687

Start time: 2011-08-22 and time is 12AM
End time: 2011-08-21 and time is 12AM

Now I have to send it as a Timestamp format to my SQL SELECT statement and also calculate the previous day. Anyone please help me what java classes I can use here to get the start data and end date.

Thank you very much for your help.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need String representation alone you can do it with SimpleDateFormat class like below.

Can you check if your start time & end time should be swapped in logic?
 
San Smith
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you John Jai for your response. Actually I am writing this code for a batch job. Where I have to retrieve only those rows from database, which were created within last 24 hours. This is my requirement.

So if today is August 22, 2011, I have to retrieve only those rows which were created August 21, 2011. Please let me know if you have any other question.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When working with complex data types, such as date, it's best to use the raw format and avoid strings if possible. For example, you can add an index to sort a column on a date making range queries, common in date queries, fast. Converting such values to strings, though, may trip up the query optimizer.

You can use a PreparedStatement and call setTimestamp() to set a value for your WHERE clause. In this case, I would use a Calendar object to get the date/time of the startTime you want to work with, 24 hours ago, and return all records > than it.
 
San Smith
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you John Jai and Scott Selikoff. I used calendar object to calculate my start date and end date and formatted it with SimpleDateFormat. Everything is working fine. Thank you very much for the information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic