Question: Revise Programming Exercise 2.8 to display the hour using a 12-hour clock. Here is a sample run: Listing 2.7, ShowCurrentTime.java, gives a program that displays

Revise Programming Exercise 2.8 to display the hour using a 12-hour clock. Here is a sample run:

Enter the time zone offset to GMT: -5 The current time is

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:

4:50:34 AM -Enter 1 public class ShowCurrentTime { 2 public static void

Enter the time zone offset to GMT: -5 The current time is 4:50:34 AM -Enter 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.33 Rating (168 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Program plan Prompt the user to enter the time zone offset to GMT Calculate the current time for giv... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!