Time comparison

3

Please, I am trying to compare hours (type Open Now / Closed Now). But I can not. To save the current time, I'm using:

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

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

But it is saved as String; To get the current time I'm doing this:

 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. Could someone give me a hint how to compare time? Thank you

    
asked by anonymous 24.11.2016 / 00:01

2 answers

3

You can compare times using the after and before methods 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 if at the last time via keyboard input the hypothetical establishment is "open" or "closed" using method after of #

    
24.11.2016 / 05:28
0

I did a method here, most of them only worked when there were no morning hours

This compares even if it's late at night to keep it open too ...

Thank you :)

    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;
}
    
13.01.2018 / 17:39