Java Reference
In-Depth Information
8
double fahrenheit = input.nextDouble();
9
10 // Convert Fahrenheit to Celsius
11 double celsius = * (fahrenheit - 32 );
12 System.out.println( "Fahrenheit " + fahrenheit + " is " +
13 celsius + " in Celsius" );
14 }
15 }
( 5.0 / 9 )
divide
Enter a degree in Fahrenheit:
Fahrenheit 100.0 is 37.77777777777778 in Celsius
100
line#
fahrenheit
celsius
8
100
11
37.77777777777778
5
9
Be careful when applying division. Division of two integers yields an integer in Java.
is
integer vs. decimal division
translated to 5.0 / 9 instead of 5 / 9 in line 11, because 5 / 9 yields 0 in Java.
2.17
How would you write the following arithmetic expression in Java?
Check
Point
+
+
4
3
d (2
a )
34) -
9( a
+
bc )
+
3( r
+
a
+
bd
2.12 Case Study: Displaying the Current Time
You can invoke System.currentTimeMillis() to return the current time.
Key
Point
The problem is to develop a program that displays the current time in GMT (Greenwich Mean
Time) in the format hour:minute:second, such as 13:19:8.
The currentTimeMillis method in the System class returns the current time in mil-
liseconds elapsed since the time 00:00:00 on January 1, 1970 GMT, as shown in Figure 2.2.
This time is known as the UNIX epoch. The epoch is the point when time starts, and 1970 was
the year when the UNIX operating system was formally introduced.
VideoNote
Use operators / and %
currentTimeMillis
UNIX epoch
Elapsed
time
Time
UNIX epoch
01-01-1970
00:00:00 GMT
Current time
System.currentTimeMillis()
F IGURE 2.2 The System.currentTimeMillis() returns the number of milliseconds
since the UNIX epoch.
You can use this method to obtain the current time, and then compute the current second,
minute, and hour as follows.
1. Obtain the total milliseconds since midnight, January 1, 1970, in totalMilliseconds
by invoking System.currentTimeMillis() (e.g., 1203183068328 milliseconds).
 
 
Search WWH ::




Custom Search