How can I compare the time of the android system with a chosen time?

0

I want to send a notification when the chosen hr is equal to the selected hr. I activate the hr and place it in a TEXVIEW with this:

private void updateDisplay() {
     displayTime.setText(
new StringBuilder().append(pad(pHour)).append(":").append(pad(pMinute)));
}

But I do not know how to compare it with the Android system time, I hope you can support it as this does not let me move forward. I thought of a comparison but it only works every time I start the application, because that's where I'm captured by the current time.

    
asked by Angelica 25.10.2017 в 07:52
source

2 answers

0

The compareTo() method is used to compare objects or in the case of String types to determine the most "large" string; It is not what you need for your particular problem.

To compare 2 dates Java 8 has a better implementation to work with data types Date .

In particular your case you could work in a simple way and without resorting to external libraries, I propose the following:

public static boolean evaluarLimite(Date date1, Date date2) {
    boolean correcto = false;
    long diferencia = (Math.abs(date1.getTime() - date2.getTime())) / 1000;
    long limit = (60 * 1000) / 1000L;//limite de tiempo

    if (diferencia <= limit) {
        correcto= true;
    }
return correcto;
}

You indicate that you can base yourself on a time limit (60 seconds in my example) if the time difference between both dates is greater than your limit, then you must make the decision or execute the event that is required.

Adjust the method to measure the unit you want, take into account that what I show you above works based on seconds (millisecs / 1000) = secs.

    
answered by 25.10.2017 в 08:29
0

Your question is not entirely clear. I suppose you have an hour, say in a textView, which does not indicate where it is taken from and you say you want to compare it with the system time.

In the code that you put, I'm surprised you use a StringBuilder to get the time ... But well, that's not a problem. Just to say that there are other, safer ways to get dates and times ...

Since the issues of time and date of the system could be used on more than one occasion, in different parts of the App, I usually use a class Utils in which I have those methods that one usually needs with more or less frequently (obtaining the current date, the current time, etc.).

Let's see for example the method that obtains the current time of the system. This method receives a strFormato parameter, to make it flexible and display the time in any desired format when calling it.

Method in an auxiliary class

public final class Utils {
   /**
     * Método que devuelve la hora del sistema <br />
     *
     * @param strFormato Formato de la hora deseado
     * @return strHora Cadena con la hora formateada.
     */

    public static String getHora(String strFormato) {

        Calendar objCalendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormato);

        String strHora = simpleDateFormat.format(objCalendar.getTime());
        return strHora;

    }
}

Example of use, where you want to compare the time

    String strHoraTuya="11:40";
    String strHoraSistema=Utils.getHora("HH:mm"); //Obtenida con el método

Then to compare, you would do it as two chains compare:

if (strHoraTuya.equals(strHoraSistema))
{

//iguales

}else{

//diferentes

}

This comparison would be launched from the action where you decide to compare the time data and the current time of the system: the click of a button, the start of an activity, etc.

    
answered by 25.10.2017 в 11:49