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.
Related
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
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.
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.
I'm from Egypt and my time is +2 GMT , but my on-line server is on -5 GMT
so when calling some queries fro database to get time for 2 AM ! I got the data from Yesterday not for today
so I'm asking how to fix such problem using mySQL statement or Java code ?
for example when I called :
SELECT DAYOFWEEK(CURDATE())
It getting right value 3 , but if I called it on 3 AM local Time here in Egypt (1 GMT) it get it 2 ! .
The best way to handle time within a database is to always store the "UTC" time (even if you have to convert from local time when storing a timestamp). Then, when you retrieve the time you convert it to the "local time" of the client (browser usually in today's modern world) by applying the locale information.
If you use this process, you eliminate having to know where your server is located (and with hosting providers sometimes in locations you do not know ...).
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?