Datetime Class

Contains methods for the Datetime primitive data type.

System

Apex supports both implicit and explicit casting of Date values to Datetime, with the time component being zeroed out in the resulting Datetime value. For more information about the Datetime, see Datetime Data Type.

The following are methods for Datetime.

  • addDays(additionalDays)

    Adds the specified number of days to a Datetime.

  • addHours(additionalHours)

    Adds the specified number of hours to a Datetime.

  • addMinutes(additionalMinutes)

    Adds the specified number of minutes to a Datetime.

  • addMonths(additionalMonths)

    Adds the specified number of months to a Datetime.

  • addSeconds(additionalSeconds)

    Adds the specified number of seconds to a Datetime.

  • addYears(additionalYears)

    Adds the specified number of years to a Datetime.

  • date()

    Returns the Date component of a Datetime in the local time zone of the context user.

  • dateGMT()

    Return the Date component of a Datetime in the GMT time zone.

  • day()

    Returns the day-of-month component of a Datetime in the local time zone of the context user.

  • dayGmt()

    Returns the day-of-month component of a Datetime in the GMT time zone.

  • dayOfYear()

    Returns the day-of-year component of a Datetime in the local time zone of the context user.

  • dayOfYearGmt()

    Returns the day-of-year component of a Datetime in the GMT time zone.

  • format()

    Converts the date to the local time zone and returns the converted date as a formatted string using the locale of the context user. If the time zone cannot be determined, GMT is used.

  • format(dateFormatString)

    Converts the date to the local time zone and returns the converted date as a string using the supplied Java simple date format. If the time zone cannot be determined, GMT is used.

  • format(dateFormatString, timezone)

    Converts the date to the specified time zone and returns the converted date as a string using the supplied Java simple date format. If the supplied time zone is not in the correct format, GMT is used.

  • formatGmt(dateFormatString)

    Returns a Datetime as a string using the supplied Java simple date format and the GMT time zone.

  • formatLong()

    Converts the date to the local time zone and returns the converted date in long date format.

  • getTime()

    Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.

  • hour()

    Returns the hour component of a Datetime in the local time zone of the context user.

  • hourGmt()

    Returns the hour component of a Datetime in the GMT time zone.

  • isSameDay(dateToCompare)

    Returns true if the Datetime that called the method is the same as the specified Datetime in the local time zone of the context user.

  • millisecond()

    Return the millisecond component of a Datetime in the local time zone of the context user.

  • millisecondGmt()

    Return the millisecond component of a Datetime in the GMT time zone.

  • minute()

    Returns the minute component of a Datetime in the local time zone of the context user.

  • minuteGmt()

    Returns the minute component of a Datetime in the GMT time zone.

  • month()

    Returns the month component of a Datetime in the local time zone of the context user (1=Jan).

  • monthGmt()

    Returns the month component of a Datetime in the GMT time zone (1=Jan).

  • newInstance(milliseconds)

    Constructs a Datetime and initializes it to represent the specified number of milliseconds since January 1, 1970, 00:00:00 GMT.

  • newInstance(date, time)

    Constructs a DateTime from the specified date and time in the local time zone.

  • newInstance(year, month, day)

    Constructs a Datetime from Integer representations of the specified year, month (1=Jan), and day at midnight in the local time zone.

  • newInstance(year, month, day, hour, minute, second)

    Constructs a Datetime from Integer representations of the specified year, month (1=Jan), day, hour, minute, and second in the local time zone.

  • newInstanceGmt(date, time)

    Constructs a DateTime from the specified date and time in the GMT time zone.

  • newInstanceGmt(year, month, date)

    Constructs a Datetime from Integer representations of the specified year, month (1=Jan), and day at midnight in the GMT time zone

  • newInstanceGmt(year, month, date, hour, minute, second)

    Constructs a Datetime from Integer representations of the specified year, month (1=Jan), day, hour, minute, and second in the GMT time zone

  • now()

    Returns the current Datetime based on a GMT calendar.

  • parse(datetimeString)

    Constructs a Datetime from the given String in the local time zone and in the format of the user locale.

  • second()

    Returns the second component of a Datetime in the local time zone of the context user.

  • secondGmt()

    Returns the second component of a Datetime in the GMT time zone.

  • time()

    Returns the time component of a Datetime in the local time zone of the context user.

  • timeGmt()

    Returns the time component of a Datetime in the GMT time zone.

  • valueOf(dateTimeString)

    Returns a Datetime that contains the value of the specified string.

  • valueOf(fieldValue)

    Converts the specified object to a Datetime. Use this method to convert a history tracking field value or an object that represents a Datetime value.

  • valueOfGmt(dateTimeString)

    Returns a Datetime that contains the value of the specified String.

  • year()

    Returns the year component of a Datetime in the local time zone of the context user.

  • yearGmt()

    Returns the year component of a Datetime in the GMT time zone.

Adds the specified number of days to a Datetime.

public Datetime addDays(Integer additionalDays)

Type: Datetime

Adds the specified number of hours to a Datetime.

public Datetime addHours(Integer additionalHours)

Type: Datetime

Adds the specified number of minutes to a Datetime.

public Datetime addMinutes(Integer additionalMinutes)

Type: Datetime

Adds the specified number of months to a Datetime.

public Datetime addMonths(Integer additionalMonths)

Type: Datetime

Adds the specified number of seconds to a Datetime.

public Datetime addSeconds(Integer additionalSeconds)

Type: Datetime

Adds the specified number of years to a Datetime.

public Datetime addYears(Integer additionalYears)

Type: Datetime

Returns the Date component of a Datetime in the local time zone of the context user.

public Date date()

Type: Date

Return the Date component of a Datetime in the GMT time zone.

public Date dateGMT()

Type: Date

Returns the day-of-month component of a Datetime in the local time zone of the context user.

public Integer day()

Type: Integer

Returns the day-of-month component of a Datetime in the GMT time zone.

public Integer dayGmt()

Type: Integer

Returns the day-of-year component of a Datetime in the local time zone of the context user.

public Integer dayOfYear()

Type: Integer

For example, February 5, 2008 08:30:12 would be day 36.

Returns the day-of-year component of a Datetime in the GMT time zone.

public Integer dayOfYearGmt()

Type: Integer

Converts the date to the local time zone and returns the converted date as a formatted string using the locale of the context user. If the time zone cannot be determined, GMT is used.

public String format()

Type: String

The sample is executed in an org where the “Enable ICU Locale Formats” crucial update is enabled. See https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_forcecom_globalization_enable_icu_cruc.htm.

Converts the date to the local time zone and returns the converted date as a string using the supplied Java simple date format. If the time zone cannot be determined, GMT is used.

public String format(String dateFormatString)

  • dateFormatString: Type: String

Type: String

For more information on the Java simple date format, see Java SimpleDateFormat.

Converts the date to the specified time zone and returns the converted date as a string using the supplied Java simple date format. If the supplied time zone is not in the correct format, GMT is used.

public String format(String dateFormatString, String timezone)

  • dateFormatString: Type: String
  • timezone: Type: String Valid time zone values for the timezone argument are the time zones of the Java TimeZone class that correspond to the time zones returned by the TimeZone.getAvailableIDs method in Java. We recommend you use full time zone names, not the three-letter abbreviations.

Type: String

For more information on the Java simple date format, see Java SimpleDateFormat.

This example uses format to convert a GMT date to the America/New_York time zone and formats the date using the specified date format.

Returns a Datetime as a string using the supplied Java simple date format and the GMT time zone.

public String formatGmt(String dateFormatString)

  • dateFormatString: Type: String

Type: String

For more information on the Java simple date format, see Java SimpleDateFormat.

Converts the date to the local time zone and returns the converted date in long date format.

public String formatLong()

Type: String

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.

public Long getTime()

Type: Long

Returns the hour component of a Datetime in the local time zone of the context user.

public Integer hour()

Type: Integer

Returns the hour component of a Datetime in the GMT time zone.

public Integer hourGmt()

Type: Integer

Returns true if the Datetime that called the method is the same as the specified Datetime in the local time zone of the context user.

public Boolean isSameDay(Datetime dateToCompare)

Type: Boolean

Return the millisecond component of a Datetime in the local time zone of the context user.

public Integer millisecond()

Type: Integer

Return the millisecond component of a Datetime in the GMT time zone.

public Integer millisecondGmt()

Type: Integer

Returns the minute component of a Datetime in the local time zone of the context user.

public Integer minute()

Type: Integer

Returns the minute component of a Datetime in the GMT time zone.

public Integer minuteGmt()

Type: Integer

Returns the month component of a Datetime in the local time zone of the context user (1=Jan).

public Integer month()

Type: Integer

Returns the month component of a Datetime in the GMT time zone (1=Jan).

public Integer monthGmt()

Type: Integer

Constructs a Datetime and initializes it to represent the specified number of milliseconds since January 1, 1970, 00:00:00 GMT.

public static Datetime newInstance(Long milliseconds)

  • milliseconds: Type: Long

Type: Datetime

The returned date is in the GMT time zone.

Constructs a DateTime from the specified date and time in the local time zone.

public static Datetime newInstance(Date date, Time time)

Type: Datetime

The returned date is in the GMT time zone.

Constructs a Datetime from Integer representations of the specified year, month (1=Jan), and day at midnight in the local time zone.

public static Datetime newInstance(Integer year, Integer month, Integer day)

Type: Datetime

The returned date is in the GMT time zone.

Constructs a Datetime from Integer representations of the specified year, month (1=Jan), day, hour, minute, and second in the local time zone.

public static Datetime newInstance(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second)

Type: Datetime

The returned date is in the GMT time zone.

Constructs a DateTime from the specified date and time in the GMT time zone.

public static Datetime newInstanceGmt(Date date, Time time)

Type: Datetime

Constructs a Datetime from Integer representations of the specified year, month (1=Jan), and day at midnight in the GMT time zone

public static Datetime newInstanceGmt(Integer year, Integer month, Integer date)

Type: Datetime

Constructs a Datetime from Integer representations of the specified year, month (1=Jan), day, hour, minute, and second in the GMT time zone

public static Datetime newInstanceGmt(Integer year, Integer month, Integer date, Integer hour, Integer minute, Integer second)

Type: Datetime

Returns the current Datetime based on a GMT calendar.

public static Datetime now()

Type: Datetime

The format of the returned datetime is: 'MM/DD/YYYY HH:MM PERIOD'

Constructs a Datetime from the given String in the local time zone and in the format of the user locale.

public static Datetime parse(String datetimeString)

  • datetimeString: Type: String

Type: Datetime

The returned date is in the GMT time zone.

This example uses parse to create a Datetime from a date passed in as a string and that is formatted for the English (United States) locale. You may need to change the format of the date string if you have a different locale.

This sample is executed in an org where the “Enable ICU Locale Formats” crucial update is enabled. See Enable ICU Locale Formats (Release Update) .

Returns the second component of a Datetime in the local time zone of the context user.

public Integer second()

Type: Integer

Returns the second component of a Datetime in the GMT time zone.

public Integer secondGmt()

Type: Integer

Returns the time component of a Datetime in the local time zone of the context user.

public Time time()

Type: Time

Returns the time component of a Datetime in the GMT time zone.

public Time timeGmt()

Type: Time

Returns a Datetime that contains the value of the specified string.

public static Datetime valueOf(String dateTimeString)

  • dateTimeString: Type: String

Type: Datetime

The returned date is in the GMT time zone.

The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.

Converts the specified object to a Datetime. Use this method to convert a history tracking field value or an object that represents a Datetime value.

public static Datetime valueOf(Object fieldValue)

  • fieldValue: Type: Object

Type: Datetime

Use this method with the OldValue or NewValue fields of history sObjects, such as AccountHistory, when the field is a Date/Time field.

Returns a Datetime that contains the value of the specified String.

public static Datetime valueOfGmt(String dateTimeString)

  • dateTimeString: Type: String

Type: Datetime

The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the GMT time zone.

Returns the year component of a Datetime in the local time zone of the context user.

public Integer year()

Type: Integer

Returns the year component of a Datetime in the GMT time zone.

public Integer yearGmt()

Type: Integer