Newer Version Available

This content describes an older version of this product. View Latest

Date Class

Contains methods for the Date primitive data type.

Namespace

System

Usage

For more information on Dates, see Date Data Type.

Date Methods

The following are methods for Date.

addDays(additionalDays)

Adds the specified number of additional days to a Date.

Signature

public Date addDays(Integer additionalDays)

Parameters

additionalDays
Type: Integer

Return Value

Type: Date

Example

addMonths(additionalMonths)

Adds the specified number of additional months to a Date

Signature

public Date addMonths(Integer additionalMonths)

Parameters

additionalMonths
Type: Integer

Return Value

Type: Date

Example

addYears(additionalYears)

Adds the specified number of additional years to a Date

Signature

public Date addYears(Integer additionalYears)

Parameters

additionalYears
Type: Integer

Return Value

Type: Date

Example

day()

Returns the day-of-month component of a Date.

Signature

public Integer day()

Return Value

Type: Integer

Example

dayOfYear()

Returns the day-of-year component of a Date.

Signature

public Integer dayOfYear()

Return Value

Type: Integer

Example

daysBetween(secondDate)

Returns the number of days between the Date that called the method and the specified date.

Signature

public Integer daysBetween(Date secondDate)

Parameters

secondDate
Type: Date

Return Value

Type: Integer

Usage

If the Date that calls the method occurs after the secondDate, the return value is negative.

Example

daysInMonth(year, month)

Returns the number of days in the month for the specified year and month (1=Jan).

Signature

public static Integer daysInMonth(Integer year, Integer month)

Parameters

year
Type: Integer
month
Type: Integer

Return Value

Type: Integer

Example

The following example finds the number of days in the month of February in the year 1960.

format()

Returns the Date as a string using the locale of the context user

Signature

public String format()

Return Value

Type: String

Example

isLeapYear(year)

Returns true if the specified year is a leap year.

Signature

public static Boolean isLeapYear(Integer year)

Parameters

year
Type: Integer

Return Value

Type: Boolean

Example

isSameDay(dateToCompare)

Returns true if the Date that called the method is the same as the specified date.

Signature

public Boolean isSameDay(Date dateToCompare)

Parameters

dateToCompare
Type: Date

Return Value

Type: Boolean

Example

month()

Returns the month component of a Date (1=Jan).

Signature

public Integer month()

Return Value

Type: Integer

Example

monthsBetween(secondDate)

Returns the number of months between the Date that called the method and the specified date, ignoring the difference in days.

Signature

public Integer monthsBetween(Date secondDate)

Parameters

secondDate
Type: Date

Return Value

Type: Integer

Example

newInstance(year, month, day)

Constructs a Date from Integer representations of the year, month (1=Jan), and day.

Signature

public static Date newInstance(Integer year, Integer month, Integer day)

Parameters

year
Type: Integer
month
Type: Integer
day
Type: Integer

Return Value

Type: Date

Example

The following example creates the date February 17th, 1960:

parse(stringDate)

Constructs a Date from a String. The format of the String depends on the local date format.

Signature

public static Date parse(String stringDate)

Parameters

stringDate
Type: String

Return Value

Type: Date

Example

The following example works in some locales.

today()

Returns the current date in the current user's time zone.

Signature

public static Date today()

Return Value

Type: Date

toStartOfMonth()

Returns the first of the month for the Date that called the method.

Signature

public Date toStartOfMonth()

Return Value

Type: Date

Example

toStartOfWeek()

Returns the start of the week for the Date that called the method, depending on the context user's locale.

Signature

public Date toStartOfWeek()

Return Value

Type: Date

Example

For example, the start of a week is Sunday in the United States locale, and Monday in European locales. For example:

valueOf(stringDate)

Returns a Date that contains the value of the specified String.

Signature

public static Date valueOf(String stringDate)

Parameters

stringDate
Type: String

Return Value

Type: Date

Usage

The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.

Example

valueOf(fieldValue)

Converts the specified object to a Date. Use this method to convert a history tracking field value or an object that represents a Date value.

Signature

public static Date valueOf(Object fieldValue)

Parameters

fieldValue
Type: Object

Return Value

Type: Date

Usage

Use this method with the OldValue or NewValue fields of history sObjects, such as AccountHistory, when the field is a Date field.

Example

This example converts history tracking fields to Date values.

Versioned Behavior Changes

Date.valueOf has been versioned in these releases.
API version 33.0 or earlier
If you call Date.valueOf with a Datetime object, the method returns a Date value that contains the hours, minutes, seconds, and milliseconds set.
API version 34.0 to API version 53.0
If you call Date.valueOf with a Datetime object, the method converts Datetime to a valid Date without the time information, but the result depends on the manner in which the Datetime object was initialized. For example, if the Datetime object was initialized using Datetime.valueOf(stringDate), the returned Date value contains time (hours) information. If the Datetime object is initialized using Datetime.newInstance(year, month, day, hour, minute, second) the returned Date value doesn’t contain time information.
API version 54.0 and later
If you call Date.valueOf with a Datetime object, the method converts the object to a valid Date without the time information.

year()

Returns the year component of a Date

Signature

public Integer year()

Return Value

Type: Integer

Example