I know there are a lot of questions asked that are related to this but they all seems not to solve my problem. I want to check if the date on the user's device is correct, start an activity but in a case where the date on the users device is wrong, it should show an error activity that asks the user to adjust their date just like how whatsapp implemented theirs..
Update Date & Time on Your Android DeviceTap Settings to open the Settings menu. Tap Date & Time. Tap Automatic. If this option is turned off, check that the correct Date, Time and Time Zone are selected.
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); formatter. setLenient(false); try { Date date= formatter. parse("02/03/2010"); } catch (ParseException e) { //If input date is in different format or invalid. }
DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy"); assertTrue(validator. isValid("02/28/2019")); assertFalse(validator. isValid("02/30/2019")); This was the most common solution before Java 8.
If you pass a non-existing/non-real date like: '20181364' (2018/13/64) into DateTime (constructor or parse-method), no exception is thrown. Instead a calculated DateTime is returned.
You must have server's timestamps to determine if the time in device is fake.
Npt pool is a free service to help you get true timestamps.
To use, device must ONLINE, you can not check without the Internet.
Copy class SntpClient to your project: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/SntpClient.java
Code:
SntpClient client = new SntpClient();
long now = -1;
if (client.requestTime("pool.ntp.org", TIME_OUT)) {
now = client.getNtpTime();
if (Math.abs(now - System.currentTimeMillis()) >= ONE_DAY){
// the device's time is wrong
startErrorActivity();
...
} else {
// the different time lower than 1 day
startNextActivity();
}
} else {
// something wrong, can't get server's time
}
Don't forget add INTERNET
permission to your manifest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With