• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

java.sql.Timestamp question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 4342
40
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic