aspose file tools*
The moose likes Java in General and the fly likes TimeStamp.toString() does not work as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "TimeStamp.toString() does not work as expected" Watch "TimeStamp.toString() does not work as expected" New topic
Author

TimeStamp.toString() does not work as expected

Ashu Bharadwaj
Greenhorn

Joined: Nov 16, 2010
Posts: 20
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Nov 16, 2010
Posts: 20
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

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TimeStamp.toString() does not work as expected