Newer Version Available
Date Class
Namespace
Usage
For more information on Dates, see Date Data Type.
Date Methods
The following are methods for Date.
day()
Signature
public Integer day()
Return Value
Type: Integer
Example
dayOfYear()
Signature
public Integer dayOfYear()
Return Value
Type: Integer
Example
daysBetween(secondDate)
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)
Signature
public static Integer daysInMonth(Integer year, Integer month)
Return Value
Type: Integer
Example
The following example finds the number of days in the month of February in the year 1960.
format()
Signature
public String format()
Return Value
Type: String
Example
month()
Signature
public Integer month()
Return Value
Type: Integer
Example
newInstance(year, month, day)
Signature
public static Date newInstance(Integer year, Integer month, Integer day)
Return Value
Type: Date
Example
The following example creates the date February 17th, 1960:
today()
Signature
public static Date today()
Return Value
Type: Date
toStartOfMonth()
Signature
public Date toStartOfMonth()
Return Value
Type: Date
Example
toStartOfWeek()
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(fieldValue)
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
- 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()
Signature
public Integer year()
Return Value
Type: Integer