views:

66

answers:

3

I'm developing a client-server application on java. The server offers some operations through a web service. The application must show some content depending on the user's custom date and time; information that is retrieved from the web service. Different users may be in different time zones.

I've been thinking on how to store on the server the user's custom time zone and provide the user the right content, for example, when the operation getTodayEvents is invoked.

What's the best practices to accoplish this in java?

+2  A: 

Yes, You can store the timezone info into your user's details table. And according to that you can serve from your web service. I think this is the best way to do it.

Krunal
A: 

There are two ways to do this:

1) Get the user to select a timezone in their account setup, and store that with the user details.

The problem with this approach is that the user login from different timezones, dealing with daylight saving time can be complicated, and you have to implement a user-friendly mechanism for selecting time zone.

2) Put the following or something like it into the login page, and have the submit action send the value of tzo to the server as a (hidden) form parameter.

<script type="text/javascript" language="javascript">
<!--
   var tzo=(new Date().gettimezoneOffset()/60)*(-1);
   // -->
</script>

This neatly avoids the problems of the first approach. The only problem is that it won't work if the user has disabled Javascript.

Stephen C
A: 

A word of advice: when your code has significant date-related logic (and if it accounts for timezones, it has), spare yourself a world of pain and use Joda time. Unlike Java's unholy union of Date and Calendar, the API of Joda Time generally nudges you towards doing things the right way.

Michael Borgwardt