Handling multiple timezones in application [duplicate] - java

This question already has answers here:
Daylight saving time and time zone best practices [closed]
(30 answers)
Closed 5 years ago.
What is the best way to handle multiple time zones in the application ?
The user can select the timezone that they are in. The user can be anywhere in the world.
When displaying data the time has to be adjusted to the timezone that the user has selected.
We have devices out in the field and they will be sending data, alerts etc.
What would be the best way to store data in database ?
Once data is stored then displaying will be straight forward.

I would suggest storing all times in UTC format. Perform all your calculations and algorithms with the UTC time. You only care about timezones for display purposes. For a particular user, convert the UTC time to their preferred timezone.

Related

Java date and time manipulation with zone using LocalDateTime [duplicate]

This question already has answers here:
Java Best Practice for Date Manipulation/Storage for Geographically Diverse Users
(2 answers)
What's the difference between Instant and LocalDateTime?
(4 answers)
Should I use Instant or DateTime or LocalDateTime in Java entities?
(3 answers)
Closed 2 days ago.
I am developing a Spring Boot app and there are some date/time fields in some entities. When considering an application will be used by multiple users from all around the world, the date and time fields should be treated properly and I am trying to follow one of the proper approach.
In this scene, what would you suggest to save date and time by users from different time zones. I think the following approaches:
I can save date and time based on UTC+0 and then when someone else retrieve data, I can show them based on their current time zone.
or save date and time based on the local time of the user with that local time zone.
I am really confused with this issue as I have no previous experience. And as far as I see, I can use LocalDateTime of java.time with Java 11+. But if you have another suggestion that is better than java.time, you can share also.
I highly recommend using the ZonedDateTime object which holds information about the date, the time, and the regarding timezone. With the Java Date and Time API introduced with Java 8 it is very easy to convert dates, times, zones, and to calculate with them. To store these objects directly, refer to your database abstraction layer you are using. If needed, you can use the DateTimeFormatter to create String representations easily to view and store.
If you need a more detailed answer, please provide a more detailed question, preferably with example code.
I will go for the first solution and give the client the time based on his/her zone. Keep in mind that the local time zone that you will display will be the time zone of the server which the client has called. To get the real time zone of the client you need to make sure to get it from the client itself. May be this link could help

Get time from Time Server using java [duplicate]

This question already has answers here:
How to use an Internet time server to get the time?
(5 answers)
Closed 6 years ago.
I am working on licensing and I need to get the current date and time from time server, that is irrespective of the time and date of the system.
I have tried Joda Time API and some classes of Java like TimeZone and I have seen that the value which I get is actually based on the system current time and date.
I have managed to get the TimeZone of the client machine to which it is set and now I want to query the time server for the current time of that TimeZone which will be not dependent on the system current date and time.
Will be helpful if I get some suggestions!
You can use TIMEAPI available here.
Alternatively, you can do some web-scraping.
www.time.is/GMT will give you current GMT time.
You can easily scrape the page with JSOUP or some other library.
FYI, the time is displayed within the element with the id of twd and the date is displayed within the element with the id dd.

How to deal with visualization of java.util.Date with a custom timezone in JSF [duplicate]

This question already has answers here:
Daylight saving time and time zone best practices [closed]
(30 answers)
Closed 7 years ago.
I have following scenario:
A JSF application that can be used by a user to create tickets.
These tickets have a validity (from-to)
I have a application configuration which states in which TimeZone/ZoneID the context of the application runs.
I need to persist the validity of the ticket and be aware that the application configuration for TimeZone/ZoneID may change and therefore also previously created tickets should be visualized with the correct converted datetime
I'd like to know what the way to go would be? Having some custom converter to store the, adding the TimeZone information as additional field in the database or some other approach?
Is this topic database vendor specific or is there some sort of generic solution?
I'm a little puzzled, maybe I'm intending to do something that one shouldn't do, cause I didn't find too much information about this topic in general.
Finally I found a different solution, based on the fact that it is not necessary to keep the original timezone the ticket was created for. I now simply use java.util.Date in the backing bean for the date picker component and convert it to an java.time.Instant as soon as I push it to the backend.
For visualization purposes on the JSF frontend I just format the java.util.Date with a SimpleDateFormatter where I set the timezone to the one configured in the backend
String timeZoneOffset = "GMT+0100";
TimeZone timeZone = TimeZone.getTimeZone(timeZoneOffset);
DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
dateTimeFormat.setTimeZone(this.timeZone);
To display the formatted java.util.Date objects in JSF I use a formatter like that
<p:outputLabel id="tripEndDate" value="#{registerBean.trip.tripEndDate}">
<f:convertDateTime pattern="yyyy-MM-dd HH:mm Z" timeZone="#{registerBean.timeZone.getID()}" />
</p:outputLabel>
Maybe as further input that might be interesting here is that I had some issues with storing the Timestamps to DB with Hibernate as Hibernate uses the systems default timezone, so if your server doesn't run in UTC you will observe this timezone conversions

How to Make/Set a time limit by date for my java program [duplicate]

This question already has answers here:
How to create a trial version of a Java program [closed]
(10 answers)
Closed 7 years ago.
I am working with java application programs,especially billing software.
I want to gave my application program as demo version for customers.
So I want to set my program for a limited period of time(30 days Trial),
and after that It should ask for a valid key for continuing.
How is it possible to Set a 30 days time period for my application in Java.
Any Idea is acceptable
Thanks in advance..
First, see the linked article about how to get and manipulate the date. This will be very useful in the future.
How to get the current date/time in Java
Second, you will only have to do some simple comparisons to clarify what the current date is, versus the fixed limit for the license. Depending on how you are distributing this, you will want to implement the licenses in different ways. For instance, include a fixed start date (such as the day you distribute) and have the expiration be one month later. Your other option is to dynamically update a license key that specifies the current date, every day. This way when you send it out later you will be covered from the day you sent it out - as opposed to from a fixed date.
Take a look at TrueLicense, because just verification of the trial time and license key sometimes is not enough and it raises a few more complicated problems.

Is there another way to get a user's time zone from a HttpServletRequest object in Spring MVC? [duplicate]

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?

Categories