Check if current time is within the radius set in the database

Asked

Viewed 53 times

1

I wanted to know how 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();
}

1 answer

0

The sqlite supports the team in queries, but it searches data in hours, minutes and seconds (HH:mm:ss) so if you pass this format (HH:mm) 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 inside a cursor:

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

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

  • I’m using Firebase, sorry I forgot to tell you

  • I had experiences with date and time formatted in firebase and were not very good, mainly for queries. I recommend working with the hour in milliseconds.

  • Your opinion is of great help, thanks for the tip

Browser other questions tagged

You are not signed in. Login or sign up in order to post.