-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeUntil.java
More file actions
109 lines (91 loc) · 2.88 KB
/
TimeUntil.java
File metadata and controls
109 lines (91 loc) · 2.88 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.Scanner;
import java.time.LocalDateTime;
public class TimeUntil{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int currentHour = LocalDateTime.now().getHour();
int currentMinute = LocalDateTime.now().getMinute();
int currentSecond = LocalDateTime.now().getSecond();
int futureHour, futureMinute, futureSecond;
int hourDiff, minuteDiff, secondDiff;
String futureTime;
if (currentHour > 12){
currentHour-=12;
}
System.out.println(currentHour);
System.out.println(currentMinute);
System.out.println(currentSecond);
System.out.println("Enter the future hour");
futureHour = scan.nextInt();
System.out.println("Enter the future minute");
futureMinute = scan.nextInt();
System.out.println("Enter the future second");
futureSecond = scan.nextInt();
hourDiff = futureHour - currentHour;
minuteDiff = futureMinute - currentMinute;
secondDiff = futureSecond - currentSecond;
if (hourDiff < 0){
hourDiff += 12;
}
if (minuteDiff < 0){
minuteDiff+=60;
hourDiff -= 1;
if (hourDiff < 0){
hourDiff += 12;
}
}
if (secondDiff < 0){
secondDiff+= 60;
minuteDiff -= 1;
if (minuteDiff < 0){
minuteDiff+=60;
hourDiff -= 1;
if (hourDiff < 0){
hourDiff += 12;
}
}
}
System.out.println("The difference is:");
if (hourDiff == 0)
{
if (minuteDiff == 0)
{
if (secondDiff == 0)
{
System.out.println("These are the same time");
}
else
{
System.out.println(secondDiff + " seconds");
}
}
else if (secondDiff == 0)
{
System.out.println(minuteDiff + " minutes");
}
else
{
System.out.println(minuteDiff + " minutes and " + secondDiff + " seconds");
}
}
else if (minuteDiff == 0)
{
if (secondDiff == 0)
{
System.out.println(hourDiff + " hours");
}
else
{
System.out.println(hourDiff + " hours and " + secondDiff + " seconds");
}
}
else if (secondDiff == 0)
{
System.out.println(hourDiff + " hours and " + minuteDiff + " minutes");
}
else
{
System.out.println(hourDiff + " hours, " + minuteDiff + " minutes and " + secondDiff + " seconds");
}
}
}