Compare android hours

0

I have a register where the establishment puts its working hours in variables String, Ex: horario_abertura and horario_fechamento format: 00:00 24hrs .

I need the app to compare the current time with the opening and closing times of the establishment, to let them know if it is open or closed!

Does anyone have an example?

    
asked by anonymous 09.11.2016 / 17:54

1 answer

1

You can do this:

public static final String inputFormat = "HH:mm";

private Date date;
private Date dateCompareOne;
private Date dateCompareTwo;

private String horarioabertura = "8:45";
private String horariofechamento = "18:45";

SimpleDateFormat inputParser = new SimpleDateFormat(inputFormat, Locale.US);

private void compareDates(){
    Calendar now = Calendar.getInstance();

    int hour = now.get(Calendar.HOUR_OF_DAY);
    int minute = now.get(Calendar.MINUTE);

    date = parseDate(hour + ":" + minute);
    dateCompareOne = parseDate(horarioabertura);
    dateCompareTwo = parseDate(horariofechamento);

    if ( dateCompareOne.before( date ) && dateCompareTwo.after(date)) {
        //esta aberto
    } else
        //esta fechado
}

private Date parseDate(String date) {

    try {
        return inputParser.parse(date);
    } catch (java.text.ParseException e) {
        return new Date(0);
    }
}
    
09.11.2016 / 19:14