As of JDK 1.1, the Date class should only be used as a wrapper for a date. That is, Date objects represent a specific instant in time with millisecond precision. The java.util.Calendar class should be used to convert between dates and time fields and the java.text.DateFormat class should be used to format and parse date strings. The corresponding methods in the JDK 1.0 version of java.util.Date are deprecated.
Calendar and its subclasses are useful for doing various manipulations with time values. Arithmetic can be performed on a Calendar's fields and the resulting date determined. A Calendar can also take "funny" dates like the 35th day of the 2nd month and normalize them. The following example shows how GregorianCalendar is used to determine if today is a weekday.
TimeZone tz = TimeZone.getDefault();Note - Calendar does not handle text representations of dates. That is the job of DateFormat. However, Calendar is used by DateFormat.
// this constructor initializes the new GregorianCalendar
// object with System.currentTimeMillis()
GregorianCalendar calendar = new GregorianCalendar(tz);
calendar.computeFields();
int dayOfWeek = calendar.get( Calendar.DAYOFWEEK );
if( dayOfWeek == Calendar.SATURDAY
|| dayOfWeek == Calendar.SUNDAY ) {
System.out.println("Weekend!");
} else {
System.out.println("Weekday");
}
Note that in order to correctly handle historical daylight savings time calculations, the API for the TimeZone class may need to be modified. This is being examined during the beta period. During the beta period, developers are encouraged to not use TimeZone directly but take advantage of it through the Calendar class.
java-intl@java.sun.com Copyright © 1996 Sun Microsystems, Inc. All rights reserved.