Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java has a method that will give you the current time. You can call it by first

ID: 3743413 • Letter: J

Question

Java has a method that will give you the current time. You can call it by first typing this in to the REPL:

Afterward, get the current time by calling:

The only problem... it gives you the current time as the number of milliseconds since midnight UTC (Links to an external site.)Links to an external site., January first, 1970! Note that it also gives it to you as a long integer (because this number is too large to fit in to a normal integer).

Develop a compound expression that includes a call to this function and evaluates to the current hour. Experiment with JShell to get it right (note that the hour should match the clock on your computer if you have the expression right).

Once you've developed an expression that you believe works correctly, submit it in the textbox below.

Another note: This method may or may not account for leap seconds that have taken place since 1970 depending on the operating system Java is running on (documented here (Links to an external site.)Links to an external site.). Do not worry about accounting for this in your expression.

Explanation / Answer

Below is the required expression with explanation to convert milliseconds retrieved from System.currentTimeMillis() call into hours in Eastern Standard Time (EST)

/**

* below is the required expression for extracting the current hour

* using System.currentTimeMillis(). The hour is displayed in Eastern

* Standard Time (EST). If you want to convert it into another timezone,

* find the required hours offset from internet and replace the above

* variable. the working of this method is as follows.

*

* @System.currentTimeMillis() returns the current time in milliseconds

*                             since Jan, 1, 1970.

* @milliseconds are converted to seconds by dividing by 1000

* @seconds are converted to hours by dividing by 3600 (seconds/60/60)

* @hours value is then added an offset value to convert time from UTC

*        to EST (remove it if you dont need it) and finally wrapped

*        under 24 to represent hours in 24 hour format, change it to 12

*        to represent in 12 hour format

*/

int currentHour = (int) (((System.currentTimeMillis() / 1000) / 3600 + HOURS_OFFSET) % 24);

System.out.println(currentHour);

Just drop a comment if you have any doubts. Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote