Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Date

public class Date
extends Object implements Serializable, Cloneable, Comparable<Date>

java.lang.Object
   ↳ java.util.Date
Known Direct Subclasses


A specific moment in time, with millisecond precision. Values typically come from currentTimeMillis(), and are always UTC, regardless of the system's time zone. This is often called "Unix time" or "epoch time".

Instances of this class are suitable for comparison, but little else. Use DateFormat to format a Date for display to a human. Use Calendar to break down a Date if you need to extract fields such as the current month or day of week, or to construct a Date from a broken-down time. That is: this class' deprecated display-related functionality is now provided by DateFormat, and this class' deprecated computational functionality is now provided by Calendar. Both of these other classes (and their subclasses) allow you to interpret a Date in a given time zone.

Note that, surprisingly, instances of this class are mutable.

Summary

Public constructors

Date()

Initializes this Date instance to the current time.

Date(int year, int month, int day)

This constructor was deprecated in API level 1. Use GregorianCalendar(int, int, int) instead.

Date(int year, int month, int day, int hour, int minute)

This constructor was deprecated in API level 1. Use GregorianCalendar(int, int, int, int, int) instead.

Date(int year, int month, int day, int hour, int minute, int second)

This constructor was deprecated in API level 1. Use GregorianCalendar(int, int, int, int, int, int) instead.

Date(long milliseconds)

Initializes this Date instance using the specified millisecond value.

Date(String string)

This constructor was deprecated in API level 1. Use DateFormat instead.

Public methods

static long UTC(int year, int month, int day, int hour, int minute, int second)

This method was deprecated in API level 1. Use code like this instead: Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); cal.set(year + 1900, month, day, hour, minute, second); cal.getTime().getTime();

boolean after(Date date)

Returns if this Date is after the specified Date.

boolean before(Date date)

Returns if this Date is before the specified Date.

Object clone()

Returns a new Date with the same millisecond value as this Date.

int compareTo(Date date)

Compare the receiver to the specified Date to determine the relative ordering.

boolean equals(Object object)

Compares the specified object to this Date and returns if they are equal.

int getDate()

This method was deprecated in API level 1. Use Calendar.get(Calendar.DATE) instead.

int getDay()

This method was deprecated in API level 1. Use Calendar.get(Calendar.DAY_OF_WEEK) instead.

int getHours()

This method was deprecated in API level 1. Use Calendar.get(Calendar.HOUR_OF_DAY) instead.

int getMinutes()

This method was deprecated in API level 1. Use Calendar.get(Calendar.MINUTE) instead.

int getMonth()

This method was deprecated in API level 1. Use Calendar.get(Calendar.MONTH) instead.

int getSeconds()

This method was deprecated in API level 1. Use Calendar.get(Calendar.SECOND) instead.

long getTime()

Returns this Date as a millisecond value.

int getTimezoneOffset()

This method was deprecated in API level 1. Use (Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / 60000 instead.

int getYear()

This method was deprecated in API level 1. Use Calendar.get(Calendar.YEAR) - 1900 instead.

int hashCode()

Returns an integer hash code for the receiver.

static long parse(String string)

This method was deprecated in API level 1. Use DateFormat instead.

void setDate(int day)

This method was deprecated in API level 1. Use Calendar.set(Calendar.DATE, day) instead.

void setHours(int hour)

This method was deprecated in API level 1. Use Calendar.set(Calendar.HOUR_OF_DAY, hour) instead.

void setMinutes(int minute)

This method was deprecated in API level 1. Use Calendar.set(Calendar.MINUTE, minute) instead.

void setMonth(int month)

This method was deprecated in API level 1. Use Calendar.set(Calendar.MONTH, month) instead.

void setSeconds(int second)

This method was deprecated in API level 1. Use Calendar.set(Calendar.SECOND, second) instead.

void setTime(long milliseconds)

Sets this Date to the specified millisecond value.

void setYear(int year)

This method was deprecated in API level 1. Use Calendar.set(Calendar.YEAR, year + 1900) instead.

String toGMTString()

This method was deprecated in API level 1. Use DateFormat instead.

String toLocaleString()

This method was deprecated in API level 1. Use DateFormat instead.

String toString()

Returns a string representation of this Date.

Inherited methods

From class java.lang.Object
From interface java.lang.Comparable

Public constructors

Date

Added in API level 1
Date ()

Initializes this Date instance to the current time.

Date

Added in API level 1
Date (int year, 
                int month, 
                int day)

This constructor was deprecated in API level 1.
Use GregorianCalendar(int, int, int) instead.

Constructs a new Date initialized to midnight in the default TimeZone on the specified date.

Parameters
year int: the year, 0 is 1900.
month int: the month, 0 - 11.
day int: the day of the month, 1 - 31.

Date

Added in API level 1
Date (int year, 
                int month, 
                int day, 
                int hour, 
                int minute)

This constructor was deprecated in API level 1.
Use GregorianCalendar(int, int, int, int, int) instead.

Constructs a new Date initialized to the specified date and time in the default TimeZone.

Parameters
year int: the year, 0 is 1900.
month int: the month, 0 - 11.
day int: the day of the month, 1 - 31.
hour int: the hour of day, 0 - 23.
minute int: the minute of the hour, 0 - 59.

Date

Added in API level 1
Date (int year, 
                int month, 
                int day, 
                int hour, 
                int minute, 
                int second)

This constructor was deprecated in API level 1.
Use GregorianCalendar(int, int, int, int, int, int) instead.

Constructs a new Date initialized to the specified date and time in the default TimeZone.

Parameters
year int: the year, 0 is 1900.
month int: the month, 0 - 11.
day int: the day of the month, 1 - 31.
hour int: the hour of day, 0 - 23.
minute int: the minute of the hour, 0 - 59.
second int: the second of the minute, 0 - 59.

Date

Added in API level 1
Date (long milliseconds)

Initializes this Date instance using the specified millisecond value. The value is the number of milliseconds since Jan. 1, 1970 GMT.

Parameters
milliseconds long: the number of milliseconds since Jan. 1, 1970 GMT.

Date

Added in API level 1
Date (String string)

This constructor was deprecated in API level 1.
Use DateFormat instead.

Constructs a new Date initialized to the date and time parsed from the specified String.

Parameters
string String: the String to parse.

Public methods

UTC

Added in API level 1
long UTC (int year, 
                int month, 
                int day, 
                int hour, 
                int minute, 
                int second)

This method was deprecated in API level 1.
Use code like this instead: Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); cal.set(year + 1900, month, day, hour, minute, second); cal.getTime().getTime();

Returns the millisecond value of the specified date and time in GMT.

Parameters
year int: the year, 0 is 1900.
month int: the month, 0 - 11.
day int: the day of the month, 1 - 31.
hour int: the hour of day, 0 - 23.
minute int: the minute of the hour, 0 - 59.
second int: the second of the minute, 0 - 59.
Returns
long the date and time in GMT in milliseconds.

after

Added in API level 1
boolean after (Date date)

Returns if this Date is after the specified Date.

Parameters
date Date: a Date instance to compare.
Returns
boolean true if this Date is after the specified Date, false otherwise.

before

Added in API level 1
boolean before (Date date)

Returns if this Date is before the specified Date.

Parameters
date Date: a Date instance to compare.
Returns
boolean true if this Date is before the specified Date, false otherwise.

clone

Added in API level 1
Object clone ()

Returns a new Date with the same millisecond value as this Date.

Returns
Object a shallow copy of this Date.

See also:

compareTo

Added in API level 1
int compareTo (Date date)

Compare the receiver to the specified Date to determine the relative ordering.

Parameters
date Date: a Date to compare against.
Returns
int an int < 0 if this Date is less than the specified Date, 0 if they are equal, and an int > 0 if this Date is greater.

equals

Added in API level 1
boolean equals (Object object)

Compares the specified object to this Date and returns if they are equal. To be equal, the object must be an instance of Date and have the same millisecond value.

Parameters
object Object: the object to compare with this object.
Returns
boolean true if the specified object is equal to this Date, false otherwise.

See also:

getDate

Added in API level 1
int getDate ()

This method was deprecated in API level 1.
Use Calendar.get(Calendar.DATE) instead.

Returns the gregorian calendar day of the month for this Date object.

Returns
int the day of the month.

getDay

Added in API level 1
int getDay ()

This method was deprecated in API level 1.
Use Calendar.get(Calendar.DAY_OF_WEEK) instead.

Returns the gregorian calendar day of the week for this Date object.

Returns
int the day of the week.

getHours

Added in API level 1
int getHours ()

This method was deprecated in API level 1.
Use Calendar.get(Calendar.HOUR_OF_DAY) instead.

Returns the gregorian calendar hour of the day for this Date object.

Returns
int the hour of the day.

getMinutes

Added in API level 1
int getMinutes ()

This method was deprecated in API level 1.
Use Calendar.get(Calendar.MINUTE) instead.

Returns the gregorian calendar minute of the hour for this Date object.

Returns
int the minutes.

getMonth

Added in API level 1
int getMonth ()

This method was deprecated in API level 1.
Use Calendar.get(Calendar.MONTH) instead.

Returns the gregorian calendar month for this Date object.

Returns
int the month.

getSeconds

Added in API level 1
int getSeconds ()

This method was deprecated in API level 1.
Use Calendar.get(Calendar.SECOND) instead.

Returns the gregorian calendar second of the minute for this Date object.

Returns
int the seconds.

getTime

Added in API level 1
long getTime ()

Returns this Date as a millisecond value. The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.

Returns
long the number of milliseconds since Jan. 1, 1970, midnight GMT.

getTimezoneOffset

Added in API level 1
int getTimezoneOffset ()

This method was deprecated in API level 1.
Use (Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / 60000 instead.

Returns the timezone offset in minutes of the default TimeZone.

Returns
int the timezone offset in minutes of the default TimeZone.

getYear

Added in API level 1
int getYear ()

This method was deprecated in API level 1.
Use Calendar.get(Calendar.YEAR) - 1900 instead.

Returns the gregorian calendar year since 1900 for this Date object.

Returns
int the year - 1900.

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for the receiver. Objects which are equal return the same value for this method.

Returns
int this Date's hash.

See also:

parse

Added in API level 1
long parse (String string)

This method was deprecated in API level 1.
Use DateFormat instead.

Returns the millisecond value of the date and time parsed from the specified String. Many date/time formats are recognized, including IETF standard syntax, i.e. Tue, 22 Jun 1999 12:16:00 GMT-0500

Parameters
string String: the String to parse.
Returns
long the millisecond value parsed from the String.

setDate

Added in API level 1
void setDate (int day)

This method was deprecated in API level 1.
Use Calendar.set(Calendar.DATE, day) instead.

Sets the gregorian calendar day of the month for this Date object.

Parameters
day int: the day of the month.

setHours

Added in API level 1
void setHours (int hour)

This method was deprecated in API level 1.
Use Calendar.set(Calendar.HOUR_OF_DAY, hour) instead.

Sets the gregorian calendar hour of the day for this Date object.

Parameters
hour int: the hour of the day.

setMinutes

Added in API level 1
void setMinutes (int minute)

This method was deprecated in API level 1.
Use Calendar.set(Calendar.MINUTE, minute) instead.

Sets the gregorian calendar minute of the hour for this Date object.

Parameters
minute int: the minutes.

setMonth

Added in API level 1
void setMonth (int month)

This method was deprecated in API level 1.
Use Calendar.set(Calendar.MONTH, month) instead.

Sets the gregorian calendar month for this Date object.

Parameters
month int: the month.

setSeconds

Added in API level 1
void setSeconds (int second)

This method was deprecated in API level 1.
Use Calendar.set(Calendar.SECOND, second) instead.

Sets the gregorian calendar second of the minute for this Date object.

Parameters
second int: the seconds.

setTime

Added in API level 1
void setTime (long milliseconds)

Sets this Date to the specified millisecond value. The value is the number of milliseconds since Jan. 1, 1970 GMT.

Parameters
milliseconds long: the number of milliseconds since Jan. 1, 1970 GMT.

setYear

Added in API level 1
void setYear (int year)

This method was deprecated in API level 1.
Use Calendar.set(Calendar.YEAR, year + 1900) instead.

Sets the gregorian calendar year since 1900 for this Date object.

Parameters
year int: the year since 1900.

toGMTString

Added in API level 1
String toGMTString ()

This method was deprecated in API level 1.
Use DateFormat instead.

Returns the string representation of this Date in GMT in the format "22 Jun 1999 13:02:00 GMT".

Returns
String

toLocaleString

Added in API level 1
String toLocaleString ()

This method was deprecated in API level 1.
Use DateFormat instead.

Returns the string representation of this Date for the default Locale.

Returns
String

toString

Added in API level 1
String toString ()

Returns a string representation of this Date. The formatting is equivalent to using a SimpleDateFormat with the format string "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999". While the current default time zone is used, all formatting and timezone names follow Locale.US. If you need control over the time zone or locale, use SimpleDateFormat instead.

Returns
String a printable representation of this object.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.