wood burning stoves*
The moose likes Beginning Java and the fly likes current time of london == ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "current time of london == ?" Watch "current time of london == ?" New topic
Author

current time of london == ?

Mayur Somani
Greenhorn

Joined: Apr 14, 2008
Posts: 26
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!
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Mayur Somani
Greenhorn

Joined: Apr 14, 2008
Posts: 26
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
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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 ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: current time of london == ?
 
Similar Threads
Displaying Current London time
stopping page by onUnload handler
dynamic data
Software for building prototypes (web based and client server)
url formating in web apps