Question: Listing 2.7, ShowCurrentTime.java, displays the current time. Improve this example to display the current date and time. The calendar example in Listing 6.12, PrintCalendar.java, should

Listing 2.7, ShowCurrentTime.java, displays the current time. Improve this example to display the current date and time. The calendar example in Listing 6.12, PrintCalendar.java, should give you some ideas on how to find the year, month, and day.

Listing 1

1 public class ShowCurrentTime { public static void main(String[] args) { //

Obtain the total milliseconds since midnight, Jan 1, 1970 long totalMilliseconds =

Listing 2

System.currentTimeMillis(); 2 3 4 // 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 %

1 public class ShowCurrentTime { public static void main(String[] args) { // Obtain the total milliseconds since midnight, Jan 1, 1970 long totalMilliseconds = System.currentTimeMillis(); 2 3 4 // 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 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Compute the current minute in the hour long currentMinute = totalMinutes % 60; // Obtain the total hours long totalHours - totalMinutes / 60; // Compute the current hour long currentHour - totalHours % 24; // Display results System.out.println("Current time is " + currentHour + + currentMinute + ":" + currentSecond + " GMT"); 28 } Current time is 17:31:8 GMT N~~6 N~~~

Step by Step Solution

3.50 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Output Current date and time is 732011 5482 GMT Java program to display c... 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!