How to compare hour system against a set time

1

I need to compare a set time against the system time, but I jump to the message "there is nothing", please help me determine what I need so that at the end I get the message "hello" when that time is in the system.

This is the code:

package jaxper.comparacion;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends Activity {


    public  String getHora(String strFormato) {

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

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

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startAlert();

    }   public void startAlert() {


        String hora_sistema = getHora("HH:mm"); //hora sistema
        String mihora = "23:57"; //hora establecida

        int timeInSec = 5;

        if ( mihora.equals(hora_sistema )){


            Toast.makeText(this, " hola ",Toast.LENGTH_SHORT).show();


        }else {Toast.makeText(this, "no hay nada ",Toast.LENGTH_SHORT).show();}
    }
}
    
asked by Alexander 03.04.2018 в 06:28
source

1 answer

0

Actually your code is correct,

public  String getHora(String strFormato) {

    Calendar objCalendar = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strFormato);
    String strHora = simpleDateFormat.format(objCalendar.getTime());
    return strHora;

}

the format also, in fact you get a format "00:00" ("HH: mm").

You just have to make sure that the call of the method to get the time ( getHora() ) is actually made at the time that the comparison is established.

If the time is equal then the Toast would be displayed with the message "hello".

    
answered by 03.04.2018 в 16:52