Skip to content

Commit 3953505

Browse files
authored
Update Exercise_*2.8.java
1 parent 05711b4 commit 3953505

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

Chapter_2/Exercise_*2.8.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
//make changes
1+
2+
/**
3+
* CurrentTime.java, gives a program that displays the current time in GMT.Revise the program so it prompts the user to
4+
* enter the time zone offset to GMT and displays the time in the specified time zone.
5+
*
6+
* Autor : Harmeet Matharoo
7+
* Date : 05 May 2021
8+
*/
9+
import java.util.Scanner;
10+
public class CurrentTime {
11+
public static void main(String [] args) {
12+
13+
14+
// Obtain the total milliseconds since midnight, Jan 1, 1970
15+
long totalMilliseconds = System.currentTimeMillis();
16+
17+
// Obtain the total seconds since midnight, Jan 1, 1970
18+
long totalSeconds = totalMilliseconds / 1000;
19+
20+
// Compute the current second in the minute in the hour
21+
long currentSecond = totalSeconds % 60;
22+
23+
// Obtain the total minutes
24+
long totalMinutes = totalSeconds / 60;
25+
26+
// Compute the current minute in the hour
27+
long currentMinute = totalMinutes % 60;
28+
29+
// Obtain the total hours
30+
long totalHours = totalMinutes / 60;
31+
32+
// Compute the current hour
33+
long currentHour = totalHours % 24;
34+
35+
36+
//Enter the timezone offset to GMT : "ask for input"
37+
Scanner input = new Scanner(System.in);
38+
39+
System.out.print("Enter the timezone offset to GMT : ");
40+
41+
int offset = input.nextInt();
42+
43+
long currentOffsetHour = (totalHours + offset) % 24;
44+
45+
46+
// Display results
47+
System.out.println("Current time is " + currentOffsetHour + ":" + currentMinute + ":" + currentSecond + " GMT");
48+
49+
}
50+
}

0 commit comments

Comments
 (0)