Newer Version Available
Date Class
Namespace
Usage
For more information on Dates, see Primitive Data Types.
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 compDate, 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, date)
Signature
public static Date newInstance(Integer year, Integer month, Integer date)
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.
year()
Signature
public Integer year()
Return Value
Type: Integer