How to compare instances of Localtime?

Asked

Viewed 226 times

0

How do I compare two variables of LocalTime in Java? I can’t use the patterns.

import java.time.DayOfWeek;
import java.time.LocalTime;

public abstract class CorrespondenteBancario {

    private DayOfWeek diasDeFuncionamento[];
    private LocalTime horarioDeAbertura;
    private LocalTime horarioDeFechamento;


    public CorrespondenteBancario(DayOfWeek diasDeFuncionamento[], LocalTime abertura, LocalTime fechamento) {
        LocalTime horarioAtual = LocalTime.now();

        if (horarioAtual>abertura && horarioAtual<fechamento) {
            ...
        }
    }

    public void depositar(double umValor, ContaBancaria umaConta) {
        // TODO Auto-generated method stub

    }

}

In which I am sending a three parameters to another class called CorrespondenteBancario.

2 answers

1

The class java.time.LocalTime has the methods isBefore and isAfter for you to compare, respectively, whether one time occurs before and after another.

That is, instead of doing t1 > t2, you do t1.isAfter(t2), and instead of using t1 < t2, is used t1.isBefore(t2):

public CorrespondenteBancario(DayOfWeek diasDeFuncionamento[], LocalTime abertura, LocalTime fechamento) {
    LocalTime horarioAtual = LocalTime.now();
    if (horarioAtual.isAfter(abertura) && horarioAtual.isBefore(fechamento)) {
        ...
    }
}

Some languages allow operators such as > and < with instances of multiple classes, but not the case with Java.


Attention, if you want to make the comparison >=, the logic is a little different. In that case would be:

if (! horarioAtual.isBefore(abertura) && ! horarioAtual.isAfter(fechamento)) {
    ...
}

Now I’ve used ! isBefore (not before), which means that the time can be later, or it can be the same time (i.e., greater or equal). It is not enough to use isAfter because it does not check the case in which they are equal. A similar logic was used to <=, that becomes ! isAfter.


Another alternative is to use compareTo, returns a negative value if the time is less than another, zero if they are equal, or a positive value if the time is greater than another:

if (horarioAtual.compareTo(abertura) > 0 && horarioAtual.compareTo(fechamento) < 0) {
    ...
}

But in that case I prefer to use isBefore and isAfter, because in my opinion it makes the code clearer and easier to read and understand (the only "advantage" would be that it is easier to change the above code to >= and <=).

0

Use Compareto. As in the example:

brightness_4 // Java program to demonstrate // Localtime.compareTo() method

java time import..*;

public class GFG { 
    public static void main(String[] args) 
    { 

        // create a LocalTime Objects 
        LocalTime time1 
            = LocalTime.parse("17:52:49"); 
        LocalTime time2 
            = LocalTime.parse("13:08:00"); 

        // apply compareTo() 
        int value = time1.compareTo(time2); 

        // print LocalDateTime 
        System.out.println("Int Value:" + value); 

        if (value > 0) 
            System.out.println("LocalTime1 is greater"); 
        else if (value == 0) 
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2"); 
        else
            System.out.println("LocalTime2 is greater"); 
    } 
} 

Browser other questions tagged

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