jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes java.sql.Timestamp question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "java.sql.Timestamp question " Watch "java.sql.Timestamp question " New topic

java.sql.Timestamp question

San Smith

Joined: Feb 10, 2008
Posts: 16

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

Joined: May 31, 2011
Posts: 1776
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

Joined: Feb 10, 2008
Posts: 16
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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

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.

[OCA 8 Book] [Blog]
San Smith

Joined: Feb 10, 2008
Posts: 16
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: java.sql.Timestamp question
It's not a secret anymore!