No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
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
For example, February 5, 1999 would be day 5.
dayOfYear()
Signature
public Integer dayOfYear()
Return Value
Type: Integer
Example
For example, February 5, 1999 would be day 36.
daysBetween(Date)
Signature
public Integer daysBetween(Date compDate)
Parameters
- compDate
- Type: Date
Return Value
Type: Integer
Usage
If the Date that calls the method occurs after the compDate, the return value is negative.
Example
1Date startDate =
2 Date.newInstance(2008, 1, 1);
3Date dueDate =
4 Date.newInstance(2008, 1, 30);
5Integer numberDaysDue =
6 startDate.daysBetween(dueDate);daysInMonth(Integer, Integer)
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.
1Integer numberDays =
2 date.daysInMonth(1960, 2);format()
Signature
public String format()
Return Value
Type: String
isSameDay(Date)
Signature
public Boolean isSameDay(Date compDate)
Parameters
- compDate
- Type: Date
Return Value
Type: Boolean
Example
1date myDate = date.today();
2date dueDate =
3 date.newInstance(2008, 1, 30);
4boolean dueNow = myDate.isSameDay(dueDate);month()
Signature
public Integer month()
Return Value
Type: Integer
monthsBetween(Date)
Signature
public Integer monthsBetween(Date compDate)
Parameters
- compDate
- Type: Date
Return Value
Type: Integer
Example
For example, March 1 and March 30 of the same year have 0 months between them.
newInstance(Integer, Integer, Integer)
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:
1Date myDate =
2 date.newinstance(1960, 2, 17);today()
Signature
public static Date today()
Return Value
Type: Date
toStartOfMonth()
Signature
public Date toStartOfMonth()
Return Value
Type: Date
Example
For example, July 14, 1999 returns July 1, 1999.
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:
1Date myDate = Date.today();
2Date weekStart = myDate.toStartofWeek();valueOf(String)
Signature
public static Date valueOf(String toDate)
Parameters
- toDate
- 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
1string year = '2008';
2string month = '10';
3string day = '5';
4string hour = '12';
5string minute = '20';
6string second = '20';
7string stringDate = year + '-' + month
8 + '-' + day + ' ' + hour + ':' +
9minute + ':' + second;
10
11Date myDate = date.valueOf(stringDate);valueOf(Object)
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
1List<AccountHistory> ahlist =
2 [SELECT Field,OldValue,NewValue
3 FROM AccountHistory];
4for(AccountHistory ah : ahlist) {
5 System.debug('Field: ' + ah.Field);
6 if (ah.field == 'MyDate__c') {
7 Date oldValue =
8 Date.valueOf(ah.OldValue);
9 Date newValue =
10 Date.valueOf(ah.NewValue);
11}