Time comparison

Asked

Viewed 2,053 times

3

Please, I’m trying to compare hours (like Open Now / Closed Now). But I’m not getting it. To save the current time, I’m using:

   object.put("h11", h11.getText().toString());

                                object.put("h22", h22.getText().toString());

But you’re saved as String; To catch the current time I’m doing so:

 SimpleDateFormat hora = new SimpleDateFormat("HH");

        Date data = new Date();

        Calendar cal = Calendar.getInstance();

        cal.setTime(data);
        Date data_atual = cal.getTime();

        String hora_atual = hora.format(data_atual);

It’s not working out. Could someone give me a suggestion on how to compare hours? Thank you

  • Hi Diego. Thank you. See, this "h:mm" I’ll get it from the bank. Saved as string?

  • Could show what format H11.gettext(). toString() and H22.gettext(). toString() ? Thank you!

  • Hi Thiago, I used a spinner for the user to choose the time, but it already has the timerpicker. But as I thought it would get too big, I used a spinner. H1 is the spinner of hours and min1 is the spinner of minutes.

  • Thiago, to catch the time I used like this: H12.setText(hora1.getSelectedItem(). toString()+":"+min1.getSelectedItem(). toString());

2 answers

3

You can compare times using methods after and before of java.util.Date. I made a small example in "pure Java" to illustrate how to make such a comparison:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class MainFrame {

    public final static SimpleDateFormat parser = new SimpleDateFormat("HH:mm");
    public final static String closeTime = "19:00";

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("What time is it? ");
        String presentTime = reader.next();
        boolean answer = checkIfClosed(presentTime);
        String textRepresentation = null;
        if (answer == true) {
            textRepresentation = "Nope! We're closed!";
        } else {
            textRepresentation = "It's open!";
        }
        System.out.println(textRepresentation);
        reader.close();
    }

    public static boolean checkIfClosed(String time) {
        try {
            Date present = parser.parse(time);
            Date closed = parser.parse(closeTime);
            if (present.after(closed)) {
                return true;
            }
        } catch (ParseException e) {
            // Invalid date was entered
        }
        return false;
    }

}

A field defines the closing time (closeTime), and a method (checkIfClosed) checks whether at last time via keyboard input the hypothetical establishment is "open" or "closed", using the method after of java.util.Date.

0

I made a way here, most only worked when there were no hours at dawn

that compares even if this morning to keep open too...

Agradeco :)

    public static boolean isIntervalOfHours(String horaAbre , String horaFecha) {
    String horaAtual = new SimpleDateFormat("HH:mm").format(new Date().getTime());// Pega hora atual do Sistema

    Integer horarioFecha[] = getHorAndMinute(horaFecha);
    Integer horarioAbre[]  = getHorAndMinute(horaAbre);

    if( horarioFecha[0] < horarioAbre[0] ) {
        Integer horarioAtual[] = getHorAndMinute(horaAtual);

        if(horarioAtual[0] <= horarioFecha[0]) {
            if(horarioAtual[1] >= horarioFecha[1] && horarioAtual[0] >= horarioFecha[0] ) {
                return true;
            } else {
                return false;
            }
        }  else {
            horaFecha = "23:59";
        }
    } 

    try {
        if (isComparaHora(horaAtual, horaFecha) && !isComparaHora(horaAtual, horaAbre)) {
            return true;
        } else {
            return false;
        }
    } catch (ParseException ex) {
        System.out.println("Horário Inválido...");
        return false;
    }

}
    public static Integer[] getHorAndMinute(String hora) {
    int horario, minutos;

    horario = Integer.parseInt(hora.substring(0, 2));
    minutos = Integer.parseInt(hora.substring(3, 5));

    return new Integer[]{horario, minutos};
}
private static boolean isComparaHora(String horaAtual, String horaFecha) throws ParseException {
    boolean fechaCaixa = false;
    if (!new SimpleDateFormat("HH:mm").parse(horaFecha).before(new SimpleDateFormat("HH:mm").parse(horaAtual))) {
        fechaCaixa = true;
    }
    return fechaCaixa;
}

Browser other questions tagged

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