Check if current time is in the defined radius in the database

1

I wanted to know how to do to see if the current system time is within a previously defined radius

I currently have this code, but it always returns "Closed at the moment"

   Calendar calendar = Calendar.getInstance();
   final Date horaCheck = calendar.getTime();

   try{
    String horaAbre = getmHoraAbre();
    Date hourAbre = new SimpleDateFormat("HH:mm").parse(horaAbre);
    String horaFecha = getmHoraFecha();
    Date hourFecha = new SimpleDateFormat("HH:mm").parse(horaFecha);

    if (horaCheck.after(hourAbre) && horaCheck.before(hourFecha)) {
        disponivelInfo.setText("Aberto no momento");

    } else {
        disponivelInfo.setText("Fechado no momento");
    }

} catch (Exception e){
    e.printStackTrace();
}
    
asked by anonymous 31.01.2018 / 03:00

1 answer

0

Sqlite supports the team in queries, but it fetches data in hours, minutes, and seconds (HH: mm: ss) so if you pass in this format (HH: mm) it will give error. First convert your date to the valid format and then query:

String dataFinalIn;
    String dataFinalOut;
    try {
        SimpleDateFormat formato = new SimpleDateFormat("HH:mm:ss");
        //HORA FECHAMENTO
        String horaFecha = getmHoraFecha();
        Date hourFecha = formato.parse(horaFecha);
        dataFinalIn = formato.format(hourFecha);
        //HORA ABERTURA
        String horaAbre = getmHoraAbre();
        Date hourAbre = formato.parse(horaFecha);
        dataFinalIn = formato.format(hourAbre );

    } catch (ParseException e) {
        e.printStackTrace();
    }

Now make this query within a cursor:

"select * from tabela where time(tabela.data_cadastro) between '"+dataFinalIn+"' and '"+dataFinalOut+"'"

Remembering that it has to be String in the format HH: mm: ss, so make the conversion right. Bjs

    
31.01.2018 / 12:56