File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Features new in Java 8 and the fly likes How to format java.sql.Timestamp in required format like 'HH:MM:SS PM DD-MM-YY' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "How to format java.sql.Timestamp in required format like Watch "How to format java.sql.Timestamp in required format like New topic
Author

How to format java.sql.Timestamp in required format like 'HH:MM:SS PM DD-MM-YY'

Shailesh Kumar Saxena
Greenhorn

Joined: Dec 15, 2012
Posts: 25

Working with postgresql 9.2 DB,need to store and display last login time for each user on login. I am storing login time as timestamp without zone.On each successful login I am updating the last login value in DB table. For fetching the last login value in my Action class execute method(working with struts 1.3.8) in required format, I am using..

select to_char(lastlogin,'HH:MM:SS PM DD-MM-YY') from userdetails where ......

I used PreparedStatement object, Problem is I am getting the same previous value each time for each person(the time when first time preparedstatement object query was parsed). I think to_char() method get executed once and stored its value for preparedstatement object. But using preparedstatement object is must. I want to store value in java.sql.Timestamp object and use some method of java.sql.Timestamp class to format. I have tried, but no luck. Any help?Any idea? Please let me know.


Shailesh Saxena,
Pilibhit, U.P. INDIA
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Why not retrieve it as a Timestamp and use (Simple)DateFormat to do the formatting in Java? After all, Timestamp extends Date and can therefore be formatted using DateFormat.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Shailesh Kumar Saxena
Greenhorn

Joined: Dec 15, 2012
Posts: 25

Hi Rob
I did the same and finally I got the required result. Thank you for reply.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to format java.sql.Timestamp in required format like 'HH:MM:SS PM DD-MM-YY'