-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.java
More file actions
64 lines (52 loc) · 1.65 KB
/
Timer.java
File metadata and controls
64 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class Timer {
long tempoInicial = 0, tempoFinal = 0, tempoDurado = 0, tempoDuracao = 0;
public Timer(){}
public Timer(long tempoDuracao){
this.tempoDuracao = tempoDuracao;
}
public void contarInicio(){
tempoInicial = System.currentTimeMillis();
}
public void contarFim(){
tempoFinal = System.currentTimeMillis();
}
private long calcularIntervaloTempo(){
tempoDurado = tempoFinal - tempoInicial;
return tempoDurado;
}
public long intervaloEmSegundos(){
calcularIntervaloTempo();
return tempoDurado/1000;
}
public long intervaloEmMinutos(){
calcularIntervaloTempo();
return tempoDurado/(1000*60);
}
public long intervaloEmHoras(){
calcularIntervaloTempo();
return tempoDurado/(1000*60*24);
}
public long tempoRestanteSegundos(){
calcularIntervaloTempo();
return tempoDuracao - intervaloEmSegundos();
}
public void contabilizar(long segundos) {
Timer t = new Timer(segundos);
t.contarInicio();
System.out.println("Contando " +segundos +" segundos.");
long tempoRestante = t.tempoRestanteSegundos();
t.contarFim();
tempoRestante = t.tempoRestanteSegundos();
while(tempoRestante>=0) {
if(tempoRestante > t.tempoRestanteSegundos()) {
System.out.println(tempoRestante +" segundos restantes");
tempoRestante = t.tempoRestanteSegundos();
}
t.contarFim();
}
System.out.println("Fim...");
}
public static void main(String[] args) {
new Timer().contabilizar(60);
}
}