Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TimeStamp.toString() does not work as expected

 
Ashu Bharadwaj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I created a Calendar object and assigned some date to the Calendar object. Now I constructed TimeStamp using the Calendar object. The constructed TimeStamp gets the current time of the system in millis. However, I wanted the Calendar objects's date to be assigned to the timestamp.
See the code

 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your problem with the Timestamp#toString() method, or do you wish to change the date to show 00:00:00.00 as the time Do you need to set hours minutes and seconds to 0 on the Calendar object before passing it to the Timestamp?

And which TimeStamp do you mean?There is no TimeStamp in the Oracle API but there are two Timestamp classes.
 
Ashu Bharadwaj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is your problem with the Timestamp#toString() method, or do you wish to change the date to show 00:00:00.00 as the time Do you need to set hours minutes and seconds to 0 on the Calendar object before passing it to the Timestamp?

And which TimeStamp do you mean?There is no TimeStamp in the Oracle API but there are two Timestamp classes.


Thanks for your response.

What I intend to do is that convert Calendar object to TimeStamp. In the code I created a Calendar object calendar and time it has is in GMT , e.g., 05:15:20.
(current GMT)

When I display the TimeStamp it shows : 11:45:20 , which is IST, (I want it to display 05:15:20 GMT )
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't recognise
as a valid time zone name! To find the valid GMT zones



Also, I would use SimpleDateFormat setting the time zone to format a date.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, a remark about these lines in your code:

Note that the method getTimeZone() of class TimeZone is a static method. You do not need to call that method on an instance of TimeZone (and, as James said, "GMT Time" is not a valid timezone identifier). Just do this:

Ashu Bharadwaj wrote:However, I wanted the Calendar objects's date to be assigned to the timestamp.

Your code is already doing exactly that in this line:

Ashu Bharadwaj wrote:When I display the TimeStamp it shows : 11:45:20 , which is IST, (I want it to display 05:15:20 GMT )

Yes, because Timestamp objects (just like java.util.Date) do not contain any timezone information. A Timestamp represents an "absolute" moment in time, without timezone information. When you print it with System.out.println(...);, Java will format it with the default timezone of the system (which happens to be IST on your system). If you want to display it in a different timezone, you must use a DateFormat object on which you set the desired timezone, and use that to format your Timestamp. For example:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic