We are using Tapestry at our Presentation Tier.
Now we have following situation :
Our application is going to be used by different users from different countries, with different timezones.
In our application we are using Tapestry 5.2.x datepicker,
so how can we allow date selection as per user's Timezone?
https://issues.apache.org/jira/browse/TAP5-841
the patch as per my comment does work. ie working with a string (2012-01-01) instead of a number (millis since epoch).
Related
I'm using JSP for a website, and there I have to display time. I have previously tried using LocalDate.now() and Date together with Calendar, like this:
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
But this also gave me UTC Time instead of PST. I can't set it manually with ZoneSet because all user's will have a different timezone. How can I fix this? I'm using Java 8
Java runs on the server, it cannot access the client's clock. If you want the time on the client's computer, you need to use JavaScript's Date object.
See How to get the exact local time of client?.
Another option is to allow users to specify their timezone, and use this to localize times when displayed to the user. You would typically provide a drop-down in some kind of Account Settings page.
But in either case, the time (or timezone) comes from the client. You can make guesses on the server using, for example, geolocation services, but you cannot actually know what the user's local time is from the server.
http://i41.tinypic.com/344bo77.png
From Day Cannot be Today's Date or Later.
To Date cannot be maximum than today's date & From Date.
I have to set the code in such a way that after selecting a particular date on From date, To date will be enabled.
Sample code will be helpful.
I would recommend you use Joda Time for your date calculations. With regards to enabling and disabling your date values, Swing components have setEnabled() methods which you can use to either disable or enable the component.
That being said, you did not specify what have you tried... and are requesting code... I doubt those are two steps in the right direction for using this site...
I have a Java web-application. In some places there is showing of dates. I want to display the date according to user timezone. How do I create a date for the show if I know the timezone the user?
I use the class Date.
Any help would be greatly appreciated.
There is no standard way to do this. On client side, you may capture time using Java script and send that information to the server. On server side, you can covert the time to Coordinated Universal Time.
Use GregorianCalendar rather than Date when you have any requirements besides basic display. You can create one for the current time by time-zone using new GregorianCalendar(TimeZone).
This question already has answers here:
How to detect the timezone of a client?
(4 answers)
Closed 5 years ago.
I need to convert my server time to the user's time depending on their time zone.
Is this the best way to figure out their timezone - by using the HttpServletRequest object?
Locale clientLocale = request.getLocale();
Calendar calendar = Calendar.getInstance(clientLocale);
TimeZone clientTimeZone = calendar.getTimeZone();
Unfortunately you cannot get the user's timezone from their request, as the client does not send the data. The locale in the request object is based on the user's Accept-Language header. What is the right timezone for "English"?
Two other possible approaches:
Use GeoIP on the client's IP to have a stab at their location, and look up (from tz) a close timezone
Use client-side Javascript to calculate their offset from UTC (subtract UTC from localtime), then look up timezones that match that offset (this will not be very granular as many timezones are e.g. +11 hours. You might be able to combine with the above).
There's no real good simple solution to this though -- which is why most sites will ask for the user's timezone, or display dates in a relative format (e.g. 5 hours ago).
You can't get it from the HttpServletRequest. The information is not there.
In JavaScript, however, you can get the timezone offset as follows:
var offset = new Date().getTimezoneOffset();
// ...
You can use it to send back to server as (ajax) parameter or to apply changes on the rendered HTML. I would however not strictly rely on this. It should at least be configureable by the enduser itself.
Provide an dropdown box to your customer where he can enter his correct time zone. And use the one you determined from the locale by default.
Anyway: The W3C has this questions in its W3C Web Internationalization FAQs:
Is it a good idea to use the HTTP Accept-Language header to determine the locale of the user?
I'm working on a GWT app where I need to support the following scenario:
The server is located in time zone A
The client's browser is set to time zone B
The GWT app is configured to display date/time in time zone C
Since GWT does not support Calendar and the native support for time
zones in javascript is non-existent I can't think of a nice and clean
solution to this problem.
Have any of you done something similar or do you know of any good
utils I could use?
Thanks!
In my experience, the following best practice significantly reduces complexity and confusion when dealing with dates and timezones in gwt:
Whenever operating/storing dates within the application, treat all dates as milliseconds since epoch in GMT timezone. You can store them as string or int, it doesn't really make a difference.
Whenever displaying the date to the end user, format the date using appropriate timezone.
For your case, when you create a date on the Server (timezone A) convert it to milliseconds since epoch in GMT before sending it to the Client. On the client, use DateTimeFormat (or write your own date formatter util) to convert it into either timezone B or timezone C as appropriate.
You can't change the GWT timezone, hence all java.util.Date's has the browser timezone. You will need to handle the current timezone setting manually.
I see 3 options:
You manage the timezone conversion yourself.
You override the serializer/deserializer of java.util.Date like in this post. And maybe using a custom java.util.Date implemtation, that overrides the getTimezoneOffset(). This approach requires recompilation of the GWT API!.
You implement your own Date, either by extending java.util.Date (like in option 2) or wrapping it with some timezone object. In this option CustomFieldSerializer's may still be usefull, but there is no need for recompiling the GWT API.
I would prefer option 3. At least until GWT RPC maybe someday will support for overriding the CustomFieldSerializer's
Usefull date/time formatting hints.
Dave Paroulek's answer is the right approach. If you want to see an example of this, we created widgets that work independent of TimeZone and process the values on the server-side where we have all of the TimeZone information we need.
UTCDateBox - Wrapper around the GWT DateBox and always chooses the date at midnight in GMT and represents the value as a Long instead of a Date.
UTCTimeBox - New widget that always chooses a time as millis since midnight, independent of timezone, also represented as a Long.
UTCDateTimeUtils - Server-side code that splits a Date into 2 Long values appropriate for UTCDateBox and UTCTimeBox in a given TimeZone and combines them back into a Date in a given TimeZone.
Here is an example of the date the time controls being used together.
Blog article describing their implementation.
These widgets are available on GitHub.
I'm assuming you are using RPC calls for server-client communication here. Also assuming that you don't care about timezone B, and you know what timezone C is on the server.
You have a few options here:
Calculate the desired date in the server (no Java limits on what you can do there) and send it in a String to be displayed to the client, so you don't have to do anymore transformations on the client.
or:
Calculate the offset between timezone A and C on the server, apply it to all the Date objects you are passing to the client and just display them on the client.
if for some reason none of these were valid for you
Calculate the offset, send it to the client and apply it to any Date you receive from the server by transforming to ms, adding the offset and then creating a Date object again.
see this demo project
GWT timezone demo project
I created a GWT-compatible Java version of the jsTimezoneDetect Javascript library specifically for this purpose. This should provide a (very good guess of) the timezone name purely on the client side. Feel free to try it out and let me know if it works or doesn't work for you.