Use the class java.time.LocalTime
. See more about her in this other question and answer my.
If you want to do it without using the package java.time
, see my other answer to this question.
Here is an example code:
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
class TesteDatas {
private static final DateTimeFormatter FORMATO_HORAS = DateTimeFormatter
.ofPattern("HH:mm")
.withResolverStyle(ResolverStyle.STRICT);
private static LocalTime faltando(LocalTime agora, LocalTime desejada) {
return desejada.minusHours(agora.getHour()).minusMinutes(agora.getMinute());
}
private static void mostrar(LocalTime horario, String objetivo) {
LocalTime desejada = LocalTime.parse(objetivo, FORMATO_HORAS);
LocalTime falta = faltando(horario, desejada);
System.out.println(
"Entre " + horario.format(FORMATO_HORAS)
+ " e " + desejada.format(FORMATO_HORAS)
+ ", a diferença é de " + falta.format(FORMATO_HORAS)
+ ".");
}
public static void main(String[] args) {
LocalTime agora = LocalTime.now();
mostrar(agora, "07:30");
mostrar(LocalTime.of( 5, 30), "07:30");
mostrar(LocalTime.of( 7, 10), "07:30");
mostrar(LocalTime.of( 0, 0), "07:30");
mostrar(LocalTime.of( 7, 0), "07:30");
mostrar(LocalTime.of( 7, 30), "07:30");
mostrar(LocalTime.of( 7, 31), "07:30");
mostrar(LocalTime.of(10, 0), "07:30");
mostrar(LocalTime.of(19, 30), "07:30");
mostrar(LocalTime.of(23, 59), "07:30");
mostrar(LocalTime.of( 0, 0), "23:59");
mostrar(LocalTime.of(23, 59), "00:00");
}
}
Here is the output (the first line will vary according to the time you run):
Entre 01:02 e 07:30, a diferença é de 06:28.
Entre 05:30 e 07:30, a diferença é de 02:00.
Entre 07:10 e 07:30, a diferença é de 00:20.
Entre 00:00 e 07:30, a diferença é de 07:30.
Entre 07:00 e 07:30, a diferença é de 00:30.
Entre 07:30 e 07:30, a diferença é de 00:00.
Entre 07:31 e 07:30, a diferença é de 23:59.
Entre 10:00 e 07:30, a diferença é de 21:30.
Entre 19:30 e 07:30, a diferença é de 12:00.
Entre 23:59 e 07:30, a diferença é de 07:31.
Entre 00:00 e 23:59, a diferença é de 23:59.
Entre 23:59 e 00:00, a diferença é de 00:01.
The key to calculating the difference is in the method faltando
:
private static LocalTime faltando(LocalTime agora, LocalTime desejada) {
return desejada.minusHours(agora.getHour()).minusMinutes(agora.getMinute());
}
That is, to calculate the difference between the two times, the hours and minutes are subtracted. The class LocalTime
turns around midnight in case of times that would be negative or that would exceed 23:59.
See here working on ideone.
Ah, it’s important to remember that objects like java.time.format.DateTimeFormatter
only need to be created once and can be reused at will. They are thread-safe and immutable and you can put them into static variables. This is something that does not occur with the java.util.SimpleDateFormatter
.
That class
Horario
is simply fantastic. I found it very interesting you implement the conversions along with comparisons too :)– user28595
Dude, it worked great and this class is fantastic!!! I only had to change one detail: gc.get(Calendar.HOUR for gc.get(Calendar.HOUR_OF_DAY. Congratulations @Victor Satafusa and thank you very much!!
– Edeson Bizerril
@Edeson.A.Bizerril Well noted! I’ve already edited the answer to tidy up this detail. I’ve also added the methods
getHoras()
andgetMinutos()
that you had forgotten and that you will probably need after.– Victor Stafusa
Thanks! You are 10!
– Edeson Bizerril