How to use the Date class

The associated class for Date:

  • java.util.Date

Common fields of the Calendar class:

DATE DAY_OF_MONTH DAY_OF_WEEK DAY_OF_YEAR
HOUR HOUR_OF_DAY MINUTE MONTH
SECONDS YEAR MONDAY…SUNDAY JANUARY…DECEMBER

Common constructors of the Dateclass:

Constructors Description
Date() Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
Date(longMillisecons) Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Common methods of the Date class:

Method Description
getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object
toString() Converts this Date object to a String of the form

Note: In all methods of class Date that accept or return year, month, date, hours, minutes, and seconds values, the following representations are used:

  • A year y is represented by the integer y – 1900.
  • A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
  • A date (day of month) is represented by an integer from 1 to 31 in the usual manner.
  • An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12.
  • A minute is represented by an integer from 0 to 59 in the usual manner.
  • A second is represented by an integer from 0 to 61; the values 60 and 61 occur only for leap seconds and even then only in Java implementations that actually track leap seconds correctly. Because of the manner in which leap seconds are currently introduced, it is extremely unlikely that two leap seconds will occur in the same minute, but this specification follows the date and time conventions for ISO C.

In all cases, arguments given to methods for these purposes need not fall within the indicated ranges; for example, a date may be specified as January 32 and is interpreted as meaning February 1.

Example 1: A statementthat converts a GregorianCalendar object to a Date object.

Date endDate = gregEndDate.getTime();

Example 2: A statement that gets a Date object for the current date/time

Date new = new Date();

Example 3: Statements that convert Date object to a string and long

String nowAsString = now.toString();
long nowInMS = now.getTime();

Example 4: Code that calculates the number of days beween two dates.

Date startDate = gregStartDate.getTime();
Date endDate = gregEndDate.getTime();
long srartDateMS = startDate.getTime();
long endDateMS = endDate.getTime();
long elapsedMS = endDateMS – startDateMS;
long elasedDays = elapsedMS / (24 * 60 * 60 * 1000);

Example 5: Using the Calendar Enums

Date date = new Date();
date.set(Calendar.Hour, 0);
date.set(Calendar.Minute, 0);
date.set(Calendar.Second, 0);

Description:

  • A Date object stores a date and time as the number of milliseconds since 1/1/1970. 00:00:00 GMT (Greenwich Mean Time).
  • You need to convert the GregorianCalendar object to a Date objects when you want to use the DateFormat class to format them.
  • Date objects are also useful when you want to calculate the number of milliseconds (or days) between two dates.

More information can be found here:

http://www.j2ee.me/javase/6/docs/api/java/util/Date.html