Date Class
Contains methods for the Date primitive data type.
For more information on Dates, see Date Data Type.
The following are methods for Date.
-
Adds the specified number of additional days to a Date.
-
Adds the specified number of additional months to a Date
-
Adds the specified number of additional years to a Date
-
Returns the day-of-month component of a Date.
-
Returns the day-of-year component of a Date.
-
Returns the number of days between the Date that called the method and the specified date.
-
Returns the number of days in the month for the specified year and month (1=Jan).
-
Returns the Date as a string using the locale of the context user
-
Returns
trueif the specified year is a leap year. -
Returns
trueif the Date that called the method is the same as the specified date. -
Returns the month component of a Date (1=Jan).
-
Returns the number of months between the Date that called the method and the specified date, ignoring the difference in days.
-
Constructs a Date from Integer representations of the year, month (1=Jan), and day.
-
Constructs a Date from a String. The format of the String depends on the local date format.
-
Returns the current date in the current user’s time zone.
-
Returns the first of the month for the Date that called the method.
-
Returns the start of the week for the Date that called the method, depending on the context user’s locale.
-
Returns a Date that contains the value of the specified String.
-
Converts the specified object to a
Date. Use this method to convert a history tracking field value or an object that represents aDatevalue. -
Returns the year component of a Date
Adds the specified number of additional days to a Date.
public Date addDays(Integer additionalDays)
-
additionalDays:
Type: Integer
Type: Date
Adds the specified number of additional months to a Date
public Date addMonths(Integer additionalMonths)
-
additionalMonths:
Type: Integer
Type: Date
Adds the specified number of additional years to a Date
public Date addYears(Integer additionalYears)
-
additionalYears:
Type: Integer
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)
-
year:
Type: Integer
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)
-
stringDate:
Type: String
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)
-
stringDate:
Type: String
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.valueOfwith aDatetimeobject, the method returns aDatevalue that contains the hours, minutes, seconds, and milliseconds set. - API version 34.0 to API version 53.0: If you call
Date.valueOfwith aDatetimeobject, the method convertsDatetimeto a validDatewithout the time information, but the result depends on the manner in which theDatetimeobject was initialized. For example, if theDatetimeobject was initialized usingDatetime.valueOf(stringDate), the returnedDatevalue contains time (hours) information. If theDatetimeobject is initialized usingDatetime.newInstance(year, month, day, hour, minute, second)the returnedDatevalue doesn’t contain time information. - API version 54.0 and later: If you call
Date.valueOfwith aDatetimeobject, the method converts the object to a validDatewithout the time information.
Returns the year component of a Date
public Integer year()
Type: Integer