• 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

current time of london == ?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a web application, where info about various cities is displayed. In this application, I need to display the current time of "that" city on whose page the user is. For example, if the user in on the London page, then the current time of London must be displayed. How can I do this cause my locales, and date time concepts are not so sound. Please help!
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd start by looking at the Calendar (especially GregorianCalendar), DateFormat (especially SimpleDateFormat to start with) and Date APIs in the JDK. I'd suggest simply trying a few things out and then posting back here with specific questions.
 
Mayur Somani
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Already tried out a few things before posting, for instance in my algorithm to calculate the current local time, I created a calender class object,

Calendar cc = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

But cc.getTimeInMillis(); returns IST instead of GMT.

Why?
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not a Date or Calendar guru, but I suggest looking at using the Calendar.getInstance(Timezone, Locale) method and passing in the United Kingdom Locale as well as the GMT timezone.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are mixing up the idea of a time zone and a locale.

Using a TimeZone will return the current time of the DEFAULT locale as the time in the timezone.

e.g if its 8pm in the default locale and you ask for the time for GMT you will get 8pm GMT.

You Need to supply the correct locale, (I think from what the API says, this will return the current time in the given locale, and should deal with daylight savings etc.)

Also remember that the UK is actual still in BST (until about the end of october) NOT GMT, and that GMT is equal to UTC.

Have a look at the API for calendar. Or check out JODA Time as an alternative.
[ September 01, 2008: Message edited by: Gavin Tranter ]
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Locale does not affect timezone. Take Canada for example; two common Locales are en_CA and fr_CA. This merely tells me the language and formatting of numbers and such. It doesn't tell me where in Canada, and thus the timezone, the user is. In fact, it doesn't even tell me the user actually is in Canada; just that they want things formatted using the language and formatting rules of that locale. If I traveled to Italy, I'd still keep my laptop's locale as en_US so I would get English messages formatted with typical US conventions (since I don't speak Italian and am use to seeing 1 million as 1,000,000.00 and not 1.000.000,00) But I'd likely change my computer's timezone so things would be in local time.

To do what you want, you basically need to have the client send the server its timezone, or at least its timezone offset. Then set that as the timezone for that session. There's an example of a Servlet filter that does that here. As explained at the bottom of that document, in its implementation, it has a limitation that the very first served page in the user's session will show the server times. You could work around that limitation by adding an additional filter that for new sessions servers a simple page with the needed javascript to set the cookie; that page then immediately redirects back to the originally requested page. Also the above solution requires the user to have javascript enabled on their PC.

Depending on you application, and its usage pattern, you can also have a registration page. In that page, you ask the user for their timezone and then save it, either in a cookie (less desirable since people delete them often these days) or in a server side user database. Of course the latter only works for an authenticated site where you know who the user is.

I hope that helps.
[ September 01, 2008: Message edited by: Mark Vedder ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic