Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to check if users date is valid

Tags:

date

android

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..

enter image description here

like image 475
Emmanuel Ayela Avatar asked Apr 13 '16 00:04

Emmanuel Ayela


People also ask

How do you check automatic date and time is enabled or not?

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.

How to validate date using SimpleDateFormat in Java?

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. }

How do you check if a date is valid or not in Java?

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.

How do you validate a dart date?

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.


1 Answers

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

like image 102
Khang .NT Avatar answered Oct 15 '22 04:10

Khang .NT