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.