For more information on Dates, see Primitive Data Types.
The following are methods for Date.
public Date addMonths(Integer additionalMonths)
Type: Date
date myDate = date.newInstance(1990, 11, 21); date newDate = myDate.addMonths(3); date expectedDate = date.newInstance(1991, 2, 21); system.assertEquals(expectedDate, newDate);
public Date addYears(Integer additionalYears)
Type: Date
date myDate = date.newInstance(1983, 7, 15); date newDate = myDate.addYears(2); date expectedDate = date.newInstance(1985, 7, 15); system.assertEquals(expectedDate, newDate);
public Integer day()
Type: Integer
date myDate = date.newInstance(1989, 4, 21);
Integer day = myDate.day();
system.assertEquals(21, day);
public Integer dayOfYear()
Type: Integer
date myDate = date.newInstance(1998, 10, 21);
Integer day = myDate.dayOfYear();
system.assertEquals(294, day);
public Integer daysBetween(Date secondDate)
Type: Integer
If the Date that calls the method occurs after the secondDate, the return value is negative.
Date startDate = Date.newInstance(2008, 1, 1); Date dueDate = Date.newInstance(2008, 1, 30); Integer numberDaysDue = startDate.daysBetween(dueDate);
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.
Integer numberDays = date.daysInMonth(1960, 2);
public String format()
Type: String
// In American-English locale date myDate = date.newInstance(2001, 3, 21); String dayString = myDate.format(); system.assertEquals('3/21/2001', dayString);
public Boolean isSameDay(Date dateToCompare)
Type: Boolean
date myDate = date.today(); date dueDate = date.newInstance(2008, 1, 30); boolean dueNow = myDate.isSameDay(dueDate);
public Integer month()
Type: Integer
date myDate = date.newInstance(2004, 11, 21);
Integer month = myDate.month();
system.assertEquals(11, month);
public Integer monthsBetween(Date secondDate)
Type: Integer
Date firstDate = Date.newInstance(2006, 12, 2); Date secondDate = Date.newInstance(2012, 12, 8); Integer monthsBetween = firstDate.monthsBetween(secondDate); System.assertEquals(72, monthsBetween);
public static Date newInstance(Integer year, Integer month, Integer date)
Type: Date
The following example creates the date February 17th, 1960:
Date myDate = date.newinstance(1960, 2, 17);
public static Date today()
Type: Date
public Date toStartOfMonth()
Type: Date
date myDate = date.newInstance(1987, 12, 17); date firstDate = myDate.toStartOfMonth(); date expectedDate = date.newInstance(1987, 12, 1); system.assertEquals(expectedDate, firstDate);
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:
Date myDate = Date.today(); Date weekStart = myDate.toStartofWeek();
public static Date valueOf(String stringDate)
Type: Date
The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.
string year = '2008'; string month = '10'; string day = '5'; string hour = '12'; string minute = '20'; string second = '20'; string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; Date myDate = date.valueOf(stringDate);
public static Date valueOf(Object fieldValue)
Type: Date
Use this method with the OldValue or NewValue fields of history sObjects, such as AccountHistory, when the field is a Date field.
In API version 33.0 or earlier, if you call Date.valueOf with an object that represents a Datetime, the method returns a Date value that contains the hours, minutes, and seconds. In version 34.0 and later, Date.valueOf converts the object to a valid Date without the time information. To convert a variable of type Datetime to a Date, use the Datetime.date method.
This example converts history tracking fields to Date values.
List<AccountHistory> ahlist = [SELECT Field,OldValue,NewValue FROM AccountHistory]; for(AccountHistory ah : ahlist) { System.debug('Field: ' + ah.Field); if (ah.field == 'MyDate__c') { Date oldValue = Date.valueOf(ah.OldValue); Date newValue = Date.valueOf(ah.NewValue); } }
public Integer year()
Type: Integer
date myDate = date.newInstance(1988, 12, 17); system.assertEquals(1988, myDate.year());