Java : Date with Examples

Date (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Date methods.

Warning :

  • The Date class is a legacy API. It is recommended to use the newer APIs such as Instant and LocalDate in the java.time package instead.


Summary

The class Date represents a specific instant in time, with millisecond precision.

Class diagram

final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles

final var date1 = new Date(0);
System.out.println(date1); // Wed Dec 31 16:00:00 PST 1969
System.out.println(date1.getTime()); // 0

final var date2 = new Date(4133894400000L);
System.out.println(date2); // Thu Dec 30 16:00:00 PST 2100
System.out.println(date2.getTime()); // 4133894400000

Constructors

Date ()

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

final var date = new Date();
System.out.println(date); // Fri Jun 28 23:30:30 PDT 2024

Date (int year, int month, int date)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).

Deprecated.

Date (int year, int month, int date, int hrs, int min)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min) or GregorianCalendar(year + 1900, month, date, hrs, min).

Deprecated.

Date (int year, int month, int date, int hrs, int min, int sec)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec).

Deprecated.

Date (long date)

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.

final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles

final var date1 = new Date(0);
System.out.println(date1); // Wed Dec 31 16:00:00 PST 1969
System.out.println(date1.getTime()); // 0

final var date2 = new Date(4133894400000L);
System.out.println(date2); // Thu Dec 30 16:00:00 PST 2100
System.out.println(date2.getTime()); // 4133894400000

Date (String s)

Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).

Deprecated.

Methods

boolean after (Date when)

Tests if this date is after the specified date.

final var date = new Date(1000);

final var date2 = new Date(500);
final var date3 = new Date(1000);
final var date4 = new Date(9999);

System.out.println(date.after(date2)); // true
System.out.println(date.after(date3)); // false
System.out.println(date.after(date4)); // false

boolean before (Date when)

Tests if this date is before the specified date.

final var date = new Date(1000);

final var date2 = new Date(500);
final var date3 = new Date(1000);
final var date4 = new Date(9999);

System.out.println(date.before(date2)); // false
System.out.println(date.before(date3)); // false
System.out.println(date.before(date4)); // true

Object clone ()

Return a copy of this object.

final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles

final var date = new Date(4133894400000L);
System.out.println(date); // Thu Dec 30 16:00:00 PST 2100

final var cloned = date.clone();
System.out.println(cloned); // Thu Dec 30 16:00:00 PST 2100

int compareTo (Date anotherDate)

Compares two Dates for ordering.

final var date = new Date(1000);

final var date2 = new Date(500);
final var date3 = new Date(1000);
final var date4 = new Date(9999);

System.out.println(date.compareTo(date2)); // 1
System.out.println(date.compareTo(date3)); // 0
System.out.println(date.compareTo(date4)); // -1

boolean equals (Object obj)

Compares two dates for equality.

final var date = new Date(1000);

final var date2 = new Date(500);
final var date3 = new Date(1000);
final var date4 = new Date(9999);

System.out.println(date.equals(date2)); // false
System.out.println(date.equals(date3)); // true
System.out.println(date.equals(date4)); // false

static Date from (Instant instant)

Obtains an instance of Date from an Instant object.

final var instant = Instant.ofEpochMilli(4133894400000L);
System.out.println(instant); // 2100-12-31T00:00:00Z

final var date = Date.from(instant);
System.out.println(date.getTime()); // 4133894400000

int getDate ()

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_MONTH).

Deprecated.

int getDay ()

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_WEEK).

Deprecated.

int getHours ()

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.HOUR_OF_DAY).

Deprecated.

int getMinutes ()

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MINUTE).

Deprecated.

int getMonth ()

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).

Deprecated.

int getSeconds ()

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.SECOND).

Deprecated.

long getTime ()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles

final var date = new Date(0);
System.out.println(date); // Wed Dec 31 16:00:00 PST 1969
System.out.println(date.getTime()); // 0

date.setTime(4133894400000L);
System.out.println(date); // Thu Dec 30 16:00:00 PST 2100
System.out.println(date.getTime()); // 4133894400000

int getTimezoneOffset ()

Deprecated. As of JDK version 1.1, replaced by -(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000).

Deprecated.

int getYear ()

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

Deprecated.

int hashCode ()

Returns a hash code value for this object.

final var ret1 = new Date(0).hashCode();
System.out.println(ret1); // 0

final var ret2 = new Date(1000).hashCode();
System.out.println(ret2); // 1000

final var ret3 = new Date(-10000).hashCode();
System.out.println(ret3); // 9999

static long parse (String s)

Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).

Deprecated.

void setDate (int date)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.DAY_OF_MONTH, int date).

Deprecated.

void setHours (int hours)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.HOUR_OF_DAY, int hours).

Deprecated.

void setMinutes (int minutes)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.MINUTE, int minutes).

Deprecated.

void setMonth (int month)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.MONTH, int month).

Deprecated.

void setSeconds (int seconds)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.SECOND, int seconds).

Deprecated.

void setTime (long time)

Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles

final var date = new Date(0);
System.out.println(date); // Wed Dec 31 16:00:00 PST 1969
System.out.println(date.getTime()); // 0

date.setTime(4133894400000L);
System.out.println(date); // Thu Dec 30 16:00:00 PST 2100
System.out.println(date.getTime()); // 4133894400000

void setYear (int year)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(Calendar.YEAR, year + 1900).

Deprecated.

String toGMTString ()

Deprecated. As of JDK version 1.1, replaced by DateFormat.format(Date date), using a GMT TimeZone.

Deprecated.

Instant toInstant ()

Converts this Date object to an Instant.

final var date = new Date(4133894400000L);

final var instant = date.toInstant();
System.out.println(instant); // 2100-12-31T00:00:00Z
System.out.println(instant.toEpochMilli()); // 4133894400000

String toLocaleString ()

Deprecated. As of JDK version 1.1, replaced by DateFormat.format(Date date).

Deprecated.

String toString ()

Converts this Date object to a String of the form:

final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles

final var date1 = new Date(0);
final var str1 = date1.toString();
System.out.println(str1); // Wed Dec 31 16:00:00 PST 1969

final var date2 = new Date(4133894400000L);
final var str2 = date2.toString();
System.out.println(str2); // Thu Dec 30 16:00:00 PST 2100

static long UTC (int year, int month, int date, int hrs, int min, int sec)

Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min, sec) or GregorianCalendar(year + 1900, month, date, hrs, min, sec), using a UTC TimeZone, followed by Calendar.getTime().getTime().

Deprecated.


Related posts

To top of page