Datetime Class
Contains methods for the Datetime primitive data type.
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.
-
Adds the specified number of days to a Datetime.
-
Adds the specified number of hours to a Datetime.
-
Adds the specified number of minutes to a Datetime.
-
Adds the specified number of months to a Datetime.
-
Adds the specified number of seconds to a Datetime.
-
Adds the specified number of years to a Datetime.
-
Returns the Date component of a Datetime in the local time zone of the context user.
-
Return the Date component of a Datetime in the GMT time zone.
-
Returns the day-of-month component of a Datetime in the local time zone of the context user.
-
Returns the day-of-month component of a Datetime in the GMT time zone.
-
Returns the day-of-year component of a Datetime in the local time zone of the context user.
-
Returns the day-of-year component of a Datetime in the GMT time zone.
-
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.
-
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.
-
Returns a Datetime as a string using the supplied Java simple date format and the GMT time zone.
-
Converts the date to the local time zone and returns the converted date in long date format.
-
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.
-
Returns the hour component of a Datetime in the local time zone of the context user.
-
Returns the hour component of a Datetime in the GMT time zone.
-
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.
-
Return the millisecond component of a Datetime in the local time zone of the context user.
-
Return the millisecond component of a Datetime in the GMT time zone.
-
Returns the minute component of a Datetime in the local time zone of the context user.
-
Returns the minute component of a Datetime in the GMT time zone.
-
Returns the month component of a Datetime in the local time zone of the context user (1=Jan).
-
Returns the month component of a Datetime in the GMT time zone (1=Jan).
-
Constructs a Datetime and initializes it to represent the specified number of milliseconds since January 1, 1970, 00:00:00 GMT.
-
Constructs a DateTime from the specified date and time in the local time zone.
-
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.
-
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
-
Returns the current Datetime based on a GMT calendar.
-
Constructs a Datetime from the given String in the local time zone and in the format of the user locale.
-
Returns the second component of a Datetime in the local time zone of the context user.
-
Returns the second component of a Datetime in the GMT time zone.
-
Returns the time component of a Datetime in the local time zone of the context user.
-
Returns the time component of a Datetime in the GMT time zone.
-
Returns a Datetime that contains the value of the specified string.
-
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.
-
Returns a Datetime that contains the value of the specified String.
-
Returns the year component of a Datetime in the local time zone of the context user.
-
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)
- additionalDays: Type: Integer
Type: Datetime
Adds the specified number of hours to a Datetime.
public Datetime addHours(Integer additionalHours)
- additionalHours: Type: Integer
Type: Datetime
Adds the specified number of minutes to a Datetime.
public Datetime addMinutes(Integer additionalMinutes)
- additionalMinutes: Type: Integer
Type: Datetime
Adds the specified number of months to a Datetime.
public Datetime addMonths(Integer additionalMonths)
- additionalMonths: Type: Integer
Type: Datetime
Adds the specified number of seconds to a Datetime.
public Datetime addSeconds(Integer additionalSeconds)
- additionalSeconds: Type: Integer
Type: Datetime
Adds the specified number of years to a Datetime.
public Datetime addYears(Integer additionalYears)
- additionalYears: Type: Integer
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)
- dateToCompare: Type: Datetime
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)
- year: Type: Integer
- month: Type: Integer
- day: Type: Integer
- hour: Type: Integer
- minute: Type: Integer
- second: Type: Integer
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)
- year: Type: Integer
- month: Type: Integer
- date: Type: Integer
- hour: Type: Integer
- minute: Type: Integer
- second: Type: Integer
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