Java Reference
In-Depth Information
NOTE Remember that the odds against a double six are 36:1, so on average you
only succeed once out of every six times you run the example.
DATES AND TIMES
Quite a few classes in the java.util package are involved with dates and times, including the Date class,
the Calendar class, and the GregorianCalendar class. In spite of the class name, a Date class object actu-
ally defines a particular instant in time to the nearest millisecond, measured from January 1, 1970, 00:00:00
GMT. Because it is relative to a particular instant in time, it also corresponds to a date. The Calendar class
is the base class for GregorianCalendar , which represents the sort of day/month/year calendar everybody
is used to and also provides methods for obtaining day, month, and year information from a Date object. A
Calendar object is always set to a particular date — a particular instant on a particular date to be precise —
but you can change it by various means. From this standpoint a GregorianCalendar object is more like one
of those desk calendars that just show one date, and you can flip over the days, months, or years to show
another date.
You also have the TimeZone class that defines a time zone that can be used in conjunction with a calendar
and that you can use to specify the rules for clock changes due to daylight saving time. The ramifications
of handling dates and times are immense so you are only able to dabble here, but at least you get the basic
ideas. Let's look at Date objects first.
The Date Class
A Date class object represents a given date and time. You have two Date constructors:
Date() creates an object based on the current time of your computer clock to the nearest milli-
second.
Date(long time) creates an object based on time , which is the number of milliseconds since
00:00:00 GMT on January 1, 1970.
With either constructor, you create an object that represents a specific instant in time to the nearest mil-
lisecond. Carrying dates around as the number of milliseconds since the dawn of the year 1970 won't grab
you as being incredibly user-friendly — but I come back to how you can better interpret a Date object in a
moment. The Date class provides three methods for comparing objects that return a boolean value:
after(Date earlier) returns true if the current object represents a date that's later than the
date represented by the argument and returns false otherwise.
before(Date later) returns true if the current object represents a date that's earlier than the
date represented by the argument and returns false otherwise.
equals(Object aDate) returns true if the current object and the argument represent the same
date and time and returns false otherwise.
Search WWH ::




Custom Search