Date Class
Namespace
Usage
For more information on Dates, see Date Data Type.
Date Methods
The following are methods for Date.
addMonths(additionalMonths)
Signature
public Date addMonths(Integer additionalMonths)
Parameters
- additionalMonths
- Type: Integer
Return Value
Type: Date
Example
1date myDate = date.newInstance(1990, 11, 21);
2date newDate = myDate.addMonths(3);
3date expectedDate = date.newInstance(1991, 2, 21);
4system.assertEquals(expectedDate, newDate);addYears(additionalYears)
Signature
public Date addYears(Integer additionalYears)
Parameters
- additionalYears
- Type: Integer
Return Value
Type: Date
Example
1date myDate = date.newInstance(1983, 7, 15);
2date newDate = myDate.addYears(2);
3date expectedDate = date.newInstance(1985, 7, 15);
4system.assertEquals(expectedDate, newDate);day()
Signature
public Integer day()
Return Value
Type: Integer
Example
1date myDate = date.newInstance(1989, 4, 21);
2Integer day = myDate.day();
3system.assertEquals(21, day);dayOfYear()
Signature
public Integer dayOfYear()
Return Value
Type: Integer
Example
1date myDate = date.newInstance(1998, 10, 21);
2Integer day = myDate.dayOfYear();
3system.assertEquals(294, day);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
1Date startDate = Date.newInstance(2008, 1, 1);
2Date dueDate = Date.newInstance(2008, 1, 30);
3Integer numberDaysDue = startDate.daysBetween(dueDate);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.
1Integer numberDays = date.daysInMonth(1960, 2);format()
Signature
public String format()
Return Value
Type: String
Example
1// In American-English locale
2date myDate = date.newInstance(2001, 3, 21);
3String dayString = myDate.format();
4system.assertEquals('3/21/2001', dayString);isSameDay(dateToCompare)
Signature
public Boolean isSameDay(Date dateToCompare)
Parameters
- dateToCompare
- Type: Date
Return Value
Type: Boolean
Example
1date myDate = date.today();
2date dueDate = date.newInstance(2008, 1, 30);
3boolean dueNow = myDate.isSameDay(dueDate);month()
Signature
public Integer month()
Return Value
Type: Integer
Example
1date myDate = date.newInstance(2004, 11, 21);
2Integer month = myDate.month();
3system.assertEquals(11, month);monthsBetween(secondDate)
Signature
public Integer monthsBetween(Date secondDate)
Parameters
- secondDate
- Type: Date
Return Value
Type: Integer
Example
1Date firstDate = Date.newInstance(2006, 12, 2);
2Date secondDate = Date.newInstance(2012, 12, 8);
3Integer monthsBetween = firstDate.monthsBetween(secondDate);
4System.assertEquals(72, monthsBetween);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:
1Date myDate = 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
1date myDate = date.newInstance(1987, 12, 17);
2date firstDate = myDate.toStartOfMonth();
3date expectedDate = date.newInstance(1987, 12, 1);
4system.assertEquals(expectedDate, firstDate);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(stringDate)
Signature
public static Date valueOf(String stringDate)
Parameters
- stringDate
- 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(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.
1List<AccountHistory> ahlist = [SELECT Field,OldValue,NewValue FROM AccountHistory];
2for(AccountHistory ah : ahlist) {
3 System.debug('Field: ' + ah.Field);
4 if (ah.field == 'MyDate__c') {
5 Date oldValue = Date.valueOf(ah.OldValue);
6 Date newValue = Date.valueOf(ah.NewValue);
7 }
8}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
Example
1date myDate = date.newInstance(1988, 12, 17);
2system.assertEquals(1988, myDate.year());