Finding exact Date Time in Android Studio - java

I am creating an android app and I need to get the EXACT date and time using android studio. There are many questions which have solutions to this, but I realised that all of them give the phone's time which can be edited by the user. I need the exact world-standard time.
Currently, the code I'm using is this:
DateFormat dfgmt = new java.text.SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
dfgmt.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
String nowTime = dfgmt.format(new Date());
But this is giving me the time which is at times tweaked by the user. Anyone has a way to get the exact time?

There are few different approaches that you could follow, based on whether you have access to the internet and/or GPS, and whether you want this functionality to work offline.
As others have suggested, you could use an online service to get the official time. For example, TimezoneDB provides such a REST API.
If you need to access the time offline, read the time from the GPS provider, which returns the time in UTC format. Check Location.getTime().
Use a combination of an online service to get the time once and combine it with SystemClock.elapsedRealtime() to get the actual real time after that, even if the device is offline.

You could use an external service for this. You could set up a simple REST API or use someone else's (check this post which gives an example of one Free Rest API to get current time as string (timezone irrelevant)) to make HTTP requests and get the time irrelevant of what is set on the user's phone.

You can ask the time to a remote server.
This will grant you that the time is not setted manually by the user and is not influenced by the timezone.

Use a 3rd-party NTP client library to get current time from an NTP server.
E.g. Apache Commons Net has a class for that.

Related

The ability to use the app only in a specific region (only using a code)

I develop applications in AndroidStudio. For the first time in my life, I faced a problem - I was asked to make the application possible for use only within ONE country. I've thought all my life that this is configurable via the Google Play Console, but I've been told that it's not. Maybe someone knows how to implement such a restriction using a code only? Mb some changes in a gradle files?
You can use Locale to get the current country of users.
val locale = context.resources.configuration.locales.get(0).country
And you can make a request to a server to know users' countries.
For example, you can try: https://ipinfo.io/country
This is not an all-time work solution, but it's worth trying.

How can running jre application can use custom provided date and time

My application depends upon time, so sometimes I have to move in time to future dates to do some application testing
I have one solution for this.
Change system date and time and restart the server, but I don't want to do this, this is very time-consuming task, and to change dates in future, affects my system's 365 office and tortoise HG and other applications too.
So basically want I want, I change date and time in server files or somehow tell JVM to point my fake clock, not to actual OS clock.
I know we have Mokito to mock the data, but my application is 20 years old without TTD.
I basically want to change the date and time which is Java jre is pointing to.
Or do we have a way out or API to modify the JRE time and date?
I hope you guys understand my problem if still doubt feel free to ask
Thanks for helping :)

How to keep trial period for my j2ee web application

I have developed one j2ee web application and generated .war file of it.
I have to share this war file with clients.
My problem is:
I don't want to give .war file for permanent use. Instead of to give it for trial period let's say for 30 days, so that they can know the features of application.
How to give trial period logic.
If I write logic using DB, they can reinstall the whole war file after one month and everything will start working again.
Client can also change the timing of system and then my control will go off.
I cannot access internet from application because it will be deployed only on intranet.
Please help me in any possible way of achieving this.
Thanks in advance
You very probably cannot do this perfectly (ever heard about cracked games and rooted phones?).
So it's a matter of making it difficult enough for the user to bypass your license, so that they won't bother.
The basic, simple idea is:
- Create a license for each user using your private key, that will contain the license expiry date
- Check the license for validity in your application, using a corresponding public key (both integrity and expiry date)
This will make sure the user won't get another month by just reinstalling.
Second simple step would be to write the last seen system time each time you exit the application, then check the system time on startup to make sure user didn't set the clock back (beware of daylight savings time though!).
That might get you covered for the two basic tricks to bypass the trial period. For more motivated users, you'll then need to add in more checks.

Let Java(JBoss) run on given timestamp, not synced from server (Windows). Only to be applied in test env

I'm trying to find a solution to my problem, which is only applicable in our test environments. I'm working with systems
(banking) depending on each system being on the same specific date. The development we do is solely within system integrations.
In short: Our systems generate the data, which we then adapt between our systems. Naturally, we do not hold the source
codes to the systems we have.
To my specific problem: I have a system(A) running on JBoss: JBoss_4_2_3_GA on Java JRE build 1.6.0_32-b05. I've set up
a messaging broker in WebMethods(WM) listening to a specific port on this system. When users trigger certain events in system(A), JMS
topics are created and sent to a queue. WM then do a few validations and deliver the messages to system(B).
Everything was working properly, up until recently when we noticed that system(B) was expecting a timestamp field being
on the same date as the system itself were in (the functionality we built won’t otherwise work). The problem is that system(A) does not provide such a system date option,
and the timestamp in the messages are "real" timestamps from the operating system (Windows 2008 R2). So I require
a fake date (timestamp) applied in system(A) which would be on the same date as system(B).
The first solution to this problem was simply to disable the NTP sync and set a fake timestamp in Windows running
system(A), which would solve the date issue. But doing this, we noticed that our Windows group policies stopped working,
which affected normal logins to the server. So this was not a solution we could apply.
I know that others have posted this specific issue, and solutions such as using java date alternatives as Joda-Time etc.
I don't see how I can apply mentioned changes into my system(A) due to the lack of source code.
Help/suggestions would be greatfully appreciated. Right now I’m blindly focused into changing the timestamp in Java in system(A). Perhaps there are better alternatives such as just applying the fake date in WM? I just don’t know enough about WM to do this either.
Ok, I finally found a solution to my problem. Since the lack of source code, it was really a hard shot to do anything within the application (Java/JBoss). Searching arcoss forums, I found a nifty little tool (for Windows) called "RunAsDate". This tool does exactly what I need, as it intercepts the API calls to the OS related to dates and times and gives whatever date/timestamp the user chooses!
The best thing is that it doesn't make any changes to the system (dll's), and runs either from the simple GUI (in app package) or called from command line. Of course it will take arguments if required.
I hope others can find this as helpful as I do: http://www.nirsoft.net/utils/run_as_date.html
BR
/rash

How to create a server for communication with an Android app [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to create a server which initially checks an Android applications trial period, so
on running an Android application the phones info is sent over to ther server.
The server stores this information.
Whenever the application is opened within that android phone
the server checks its trial period has or has not expired.
In a way it's like an Android time bomb.
I'm completely new to servers and really just want a basic, secure, server that simply registers a phone and compares it against a trial limit.
Anyone have any information on how I could do this?
Any code examples or tutorials are very much appreciated.
You didn't specify your server technology, but in principal you need to do the following:
You probably want to expose them as a REST Webservice. All you need is a GET operation to basically figure out if the trial has expired or not. Since you are using Android and have gained familiarity with Java, I suggest you look at JAX-RS which is one way to implement REST in Java. If you are familiar with other language, then feel free to go for that.
The simplest form of your GET URL would probably look like http://yoursite/getTrial/[beginTrialDate] where [beginTrialDate] is a date in millis since Jan 1, 1970 GMT (standard approach)
On the server side, you simply took the [beginTrialDate] and check if it has exceed your trial period by comparing current time to [beginTrialDate] + [trial period]
You would then return a simple JSON response containing the information whether the app has expired or not. The simplest form would be: { "hasExpired" : true/false }
You would call this WebService in Android using HttpClient as you would probably know already. Check this HTTP Client Tutorial
You could make the server more robust by storing the phone identifier and your GET URL change to http://yoursite/getTrial/[phoneID]. The only additional complexity is you have to look up the begin trial date by phoneID and then compare it again using the step #4
Let me know if you need more clarification and I will add it to the post
Easiest way would be write a JSON service. here is a link to a sample PHP JSON service - http://davidwalsh.name/web-service-php-mysql-xml-json
You can easily find JSON code for your choice of language.
I'm guessing that you dont need the service to return lot of data - probably a flag or minimal data. You could simply parse through the JSON string that is returned to the device. If you have lot of data to be passed, you could try some free JSON libraries available

Categories