Java Reference
In-Depth Information
24.4. Time, Dates, and Calendars
Time is represented as a
long
integer measured in milliseconds since mid-
night Greenwich Mean Time (
GMT
) January 1, 1970. This starting point
for time measurement is known as the
epoch.
This value is signed, so
negative values signify time before the beginning of the epoch. The
Sys-
tem.currentTimeMillis
method returns the current time. This value will ex-
press dates into the year
A.D.
292,280,995, which should suffice for most
purposes.
You can use
java.util.Date
to hold a time and perform some simple time-
related operations. When a new
Date
object is created, you can specify a
long
value for its time. If you use the no-arg constructor, the
Date
object
will mark the time of its creation. A
Date
object can be used for simple
operations. For example, the simplest program to print the current time
(repeated from page
37
) is
import java.util.Date;
class Date2 {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now);
}
}
This program will produce output such as the following:
Sun Mar 20 08:48:38 GMT+10:00 2005
Note that this is not localized output. No matter what the default locale,
the date will be in this format, adjusted for the current time zone.