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

GMT time displaying

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating a web application. In that I have created a table which stores the name of a City and its Time variation from GMT in seconds e.g. 2000 or -5000. Now I am finding it difficult in the Java API about GMT. Can anyone please tell me how can I convert this value into Current Time for that City.
SimpleDateFormat class have something written about GMT but I am confused...

If this is not possible than please tell me any alternate ways of calculating current time of a place. I can't use Locales as the information about cities will be added by Administrator of the site who will enter the local city time by himself....

Any help would be very appreciated...

Thanks
Ankit
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry not a useful response.

Just a question for my clarification - why GMT offset? won't java.util.SimpleTimeZone work?

Maybe your need might be filled with Joda Time if you aren't constrained from using ASF-licensed code.

regards
Kalyan
[ September 01, 2008: Message edited by: Kalyan Ram ]
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this code but I don't know why this is not working

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.add(Calendar.SECOND, timeVariation);
Date currentLocalTime = cal.getTime();

Can anyone tell me the reason....
 
Kalyan Ram
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What were the expected and actual outputs?

/k
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually TimeZone.getTimeZone("GMT") is returning GMT time zone with the current time of the server on which I am running this code...But I want the GMT time zone object with the actual GMT time which must be calculated according to the time zone of the server.

This means that I am running the code on my pc which is set to IST. now calling
Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime()

is returning the current time of my PC...
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I read on sun's forum that this problem is due to me converting Calendar to Date removes the Time zone specific information. So I changed my code to this

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.add(Calendar.SECOND, timeVariation);
Date currentLocalTime = new Date(cal.getTimeInMillis());

But still it is not working. even if leave the add aside

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentLocalTime = new Date(cal.getTimeInMillis());

Even the above code prints current time of the system instead of GMT time. I think this should have worked....
 
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

Originally posted by Ankit Garg:
I tried this code but I don't know why this is not working

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.add(Calendar.SECOND, timeVariation);
Date currentLocalTime = cal.getTime();

Can anyone tell me the reason....


Class Date does not know anything about timezones. A Date object is not in a specific timezone - a Date object only contains a number of milliseconds since 1 January 1970, 12:00 AM (GMT).

If you want to display the date and time in a specific timezone, then you must convert the Date object to a String by using a DateFormat object. You set the timezone on the DateFormat object. For example:

 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic