• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

TimeStamp.toString() does not work as expected

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 80265
429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic