Date Class

Contains methods for the Date primitive data type.

System

For more information on Dates, see Date Data Type.

The following are methods for Date.

  • addDays(additionalDays)

    Adds the specified number of additional days to a Date.

  • addMonths(additionalMonths)

    Adds the specified number of additional months to a Date

  • addYears(additionalYears)

    Adds the specified number of additional years to a Date

  • day()

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

  • dayOfYear()

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

  • daysBetween(secondDate)

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

  • daysInMonth(year, month)

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

  • format()

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

  • isLeapYear(year)

    Returns true if the specified year is a leap year.

  • isSameDay(dateToCompare)

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

  • month()

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

  • monthsBetween(secondDate)

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

  • newInstance(year, month, day)

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

  • parse(stringDate)

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

  • today()

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

  • toStartOfMonth()

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

  • toStartOfWeek()

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

  • valueOf(stringDate)

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

  • 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.

  • year()

    Returns the year component of a Date

Adds the specified number of additional days to a Date.

public Date addDays(Integer additionalDays)

Type: Date

Adds the specified number of additional months to a Date

public Date addMonths(Integer additionalMonths)

Type: Date

Adds the specified number of additional years to a Date

public Date addYears(Integer additionalYears)

Type: Date

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

public Integer day()

Type: Integer

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

public Integer dayOfYear()

Type: Integer

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

public Integer daysBetween(Date secondDate)

  • secondDate:

    Type: Date

Type: Integer

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

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

public static Integer daysInMonth(Integer year, Integer month)

Type: Integer

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

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

public String format()

Type: String

Returns true if the specified year is a leap year.

public static Boolean isLeapYear(Integer year)

Type: Boolean

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

public Boolean isSameDay(Date dateToCompare)

  • dateToCompare:

    Type: Date

Type: Boolean

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

public Integer month()

Type: Integer

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

public Integer monthsBetween(Date secondDate)

  • secondDate:

    Type: Date

Type: Integer

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

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

Type: Date

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

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

public static Date parse(String stringDate)

Type: Date

The following example works in some locales.

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

public static Date today()

Type: Date

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

public Date toStartOfMonth()

Type: Date

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

public Date toStartOfWeek()

Type: Date

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

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

public static Date valueOf(String stringDate)

Type: Date

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

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.

public static Date valueOf(Object fieldValue)

  • fieldValue:

    Type: Object

Type: Date

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

This example converts history tracking fields to Date values.

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.

Returns the year component of a Date

public Integer year()

Type: Integer