'how to show error if on device time does not match with internet time in android studio like WhatsApp

im making an android app which shows time of upload relative to the device time, but if the device time and date is not set correct then I do not get the desired result. so how to show warning or error if the device time and date is not correct or matches to the internet time. the app should not work unless the user set the date and time correct.



Solution 1:[1]

This thing works best for my apps. I use jsoup to search the google time and gets current time and then I compare the phone time with google time. So if these time are different you can stop user using a dialogbox or alertbox to tell them the times have changed. You can implement in MainActivity to check this condition. Here is a snippet so you get the idea more clearly.

public class HomeActivity extends AppCompatActivity {
    //phoneDate and phoneTime to get current phone date and time
    String phoneDate = new SimpleDateFormat("dd MMM yyyy ").format(clnd.getTime()).trim();
    String phoneTime = new SimpleDateFormat("hh:mm a").format(clnd.getTime()).trim();
    String googleDate;
    String googleTime ;
    @Override
    protected void onCreate(Bundle _savedInstanceState) {
    super.onCreate(_savedInstanceState);
    setContentView(R.layout.home);
    Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try  {
                        //URL to search time
                        String url = "https://www.google.co.in/search?q=time";

                        Document document  = Jsoup.connect(url).get();
                        org.jsoup.select.Elements time = document.getElementsByClass("gsrt vk_bk FzvWSb YwPhnf");
                        org.jsoup.select.Elements date = document.getElementsByClass("KfQeJ");

                        Log.d("HTML", "google date" + String.format(date.text()));
                        Log.d("HTML", "google time" + time.text());

                        googleDate = date.text().trim();
                        googleTime = time.text().trim();
                        //'0'is not present when hour is single digit 
                        char second = googleTime.charAt(1);
                        if(second == ':'){
                        googleTime = "0" + googleTime;
                    }
                    Log.d("Proper format", "google time" + googleTime);

                    Log.d("Date", "your current url when webpage loading.." + phoneDate);
                    Log.d("Time", "your current url when webpage loading.." + phoneTime);

                    if(googleDate.contains(phoneDate) && googleTime.equals(phoneTime)){
                        Log.d("Time", "your current url when webpage loading.." + " true");
                    }else{
                        Log.d("Time", "your current url when webpage loading.." + " false");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }   
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robin Singh