15.4. 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 actually defines a particular instant in time to the nearest millisecond, measured from January 1, 1970, 00:00:00 GMT. Since 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'll only be able to dabble here, but at least you will get the basic ideas. Let's take a look at Date objects first.
15.4.1. The Date Class
With the Date class you can create an object that represents a given date and time. You have two ways to do ...