Java Reference
In-Depth Information
2. Enter line 10 from Figure 2-33 on page 79. Press the ENTER key.
The import statement to import the Date class package from the SDK is dis-
played in line 10 (Figure 2-35). The Date class from the java.util package
will be imported into the Welcome to My Day application. Line 11 is blank.
import
statement
FIGURE 2-35
Some programmers place import statements before the block comments
rather than after them. Either placement is acceptable, provided that the import
statements are placed before the class header.
Entering Code to Call a System Date Constructor
The operating system running on your computer keeps track of the current
date and time. This system date is kept current by a battery inside the system
unit on most computers. The Date class in Java represents a specific instant in
time, measured to the nearest millisecond. Java creates a Date object and initial-
izes it so that it represents the exact date and time it was allocated.
In order for Java to ask the operating system for that system date, a pro-
grammer must create a storage location to hold the system date data. Line 16 in
Figure 2-33 on page 79 displays the code to construct the storage location
to hold the value for the Date object with the variable name, currentDate. A con-
structor , which is identified by the = new notation, declares the type of data or
object to be stored and assigns it a variable name in the computer's memory.
Programmers declare the type of data or object by entering the name of the Java
data type followed by the variable name. In line 16, Date is the data type and
currentDate is the variable.
Use of Date Constructors and Methods
Date constructors in Java are not used to set a particular date
or calculate future dates. Recent versions of the Java SDK have
deprecated, or retired, methods such as setDate() and setTime()
in favor of methods related to a newer Calendar object. However,
the Date object and its constructor remains an easy way to display
the system date on a given computer system.
Search WWH ::




Custom Search