Question: Listing 2.7, ShowCurrentTime.java, gives a program that displays the current time in GMT. Revise the program so that it prompts the user to enter the
Listing 2.7, ShowCurrentTime.java, gives a program that displays the current time in GMT. Revise the program so that it prompts the user to enter the time zone offset to GMT and displays the time in the specified time zone. Here is a sample run:
![1 public class ShowCurrentTime { 2 public static void main(String [] args)](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2022/11/636a70bf8af47_063636a70bf7ad38.jpg)
Enter the time zone offset to GMT: ?5
The current time is 4:50:34
1 public class ShowCurrentTime { 2 public static void main(String [] args) { // Obtain the total milliseconds since midnight, Jan 1, 1970 long totalMilliseconds = System.currentTimeMillis(); 3 // Obtain the total seconds since midnight, Jan 1, 1970 long totalSeconds = totalMilliseconds / 1000; // Compute the current second in the minute in the hour long currentSecond = totalSeconds % 60; 10 11 // Obtain the total minutes long totalMinutes = totalSeconds / 60; 12 13 totalMinutes 14 // Compute the current minute in the hour long currentMinute = totalMinutes % 60; 15 16 currentMinute 17 // Obtain the total hours long totalHours = totalMinutes / 60; 18 19 totalHours 20 // Compute the current hour long currentHour = totalHours % 24; 21 22 currentHour 23 // Display results System.out.println("Current t ime is " + currentHour + + currentMinute + ":" + currentSecond + " GMT"); 24 25 26 27 preparing output 28 }
Step by Step Solution
3.43 Rating (159 Votes )
There are 3 Steps involved in it
Output 5Enter the time zone offset to GMT Current time is 8376 GMT The Java program takes offset to ... View full answer
Get step-by-step solutions from verified subject matter experts
