Date Class

Contains methods for the Date primitive data type.

Namespace

System

Usage

For more information on Dates, see Primitive Data Types.

Date Methods

The following are methods for Date.

addDays(Integer)

Adds the specified number of addlDays to a Date.

Signature

public Date addDays(Integer addlDays)

Parameters

addlDays
Type: Integer

Return Value

Type: Date

Example

date myDate = 
     date.newInstance(1960, 2, 17);
date newDate = mydate.addDays(2);

addMonths(Integer)

Adds the specified number of addlMonths to a Date

Signature

public Date addMonths(Integer addlMonths)

Parameters

addlMonths
Type: Integer

Return Value

Type: Date

addYears(Integer)

Adds the specified number of addlYears to a Date

Signature

public Date addYears(Integer addlYears)

Parameters

addlYears
Type: Integer

Return Value

Type: Date

day()

Returns the day-of-month component of a Date.

Signature

public Integer day()

Return Value

Type: Integer

Example

For example, February 5, 1999 would be day 5.

dayOfYear()

Returns the day-of-year component of a Date.

Signature

public Integer dayOfYear()

Return Value

Type: Integer

Example

For example, February 5, 1999 would be day 36.

daysBetween(Date)

Returns the number of days between the Date that called the method and the specified 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

Date startDate = 
     Date.newInstance(2008, 1, 1);
Date dueDate = 
     Date.newInstance(2008, 1, 30);
Integer numberDaysDue = 
     startDate.daysBetween(dueDate);

daysInMonth(Integer, Integer)

Returns the number of days in the month for the specified year and month (1=Jan).

Signature

public static Integer daysInMonth(Integer year, Integer month)

Parameters

year
Type: Integer
month
Type: Integer

Return Value

Type: Integer

Example

The following example finds the number of days in the month of February in the year 1960.

Integer numberDays = 
        date.daysInMonth(1960, 2);

format()

Returns the Date as a string using the locale of the context user

Signature

public String format()

Return Value

Type: String

isLeapYear(Integer)

Returns true if the specified year is a leap year.

Signature

public static Boolean isLeapYear(Integer year)

Parameters

year
Type: Integer

Return Value

Type: Boolean

isSameDay(Date)

Returns true if the Date that called the method is the same as the specified date.

Signature

public Boolean isSameDay(Date compDate)

Parameters

compDate
Type: Date

Return Value

Type: Boolean

Example

date myDate = date.today();
date dueDate = 
   date.newInstance(2008, 1, 30);
boolean dueNow = myDate.isSameDay(dueDate);

month()

Returns the month component of a Date (1=Jan).

Signature

public Integer month()

Return Value

Type: Integer

monthsBetween(Date)

Returns the number of months between the Date that called the method and the specified date, ignoring the difference in dates.

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)

Constructs a Date from Integer representations of the year, month (1=Jan), and day.

Signature

public static Date newInstance(Integer year, Integer month, Integer date)

Parameters

year
Type: Integer
month
Type: Integer
date
Type: Integer

Return Value

Type: Date

Example

The following example creates the date February 17th, 1960:

Date myDate = 
        date.newinstance(1960, 2, 17);

parse(String)

Constructs a Date from a String. The format of the String depends on the local date format.

Signature

public static Date parse(String Date)

Parameters

Date
Type: String

Return Value

Type: Date

Example

The following example works in some locales.

date mydate = date.parse('12/27/2009');

today()

Returns the current date in the current user's time zone.

Signature

public static Date today()

Return Value

Type: Date

toStartOfMonth()

Returns the first of the month for the Date that called the method.

Signature

public Date toStartOfMonth()

Return Value

Type: Date

Example

For example, July 14, 1999 returns July 1, 1999.

toStartOfWeek()

Returns the start of the week for the Date that called the method, depending on the context user's locale.

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:

Date myDate = Date.today();
Date weekStart = myDate.toStartofWeek();

valueOf(String)

Returns a Date that contains the value of the specified 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

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);

valueOf(Object)

Converts the specified history tracking field value to a Date.

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

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);
}

year()

Returns the year component of a Date

Signature

public Integer year()

Return Value

Type: Integer

© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.