SAVE UP to ₹4999/- OFF On All Salesforce Courses Claim Offer

3

Time and DateTime Data Types in APEX

The Time data type in Apex is tailored for situations where only the time of day matters, independent of any specific date. It’s ideal for tracking daily routines, storing hours, or any scenario where the focus is solely on the time, not the date. The DateTime Data Type in Apex is used for indicating time with specific dates and time zones. 

What is the Time Data Type in Apex?

The Time Data Type is a value that indicates a particular time. Time values must always be created with a system static method. Time datatype stores time in hours, minutes, seconds, and milliseconds.

Time Methods

The following are methods for Time.

S.NoFunctionExample
1newInstance(hour, minutes, seconds, milliseconds): Constructs a Time from Integer representations of the specified hour, minutes, seconds, and milliseconds. (UTC is assumed.)Time specificTime = Time.newInstance(14, 30, 15, 250); // 2:30:15.250 PMSystem.debug(‘Specific Time: ‘ + specificTime);
2addHours(additionalHours): Adds the specified number of hours to a Time.Time currentTime = Time.newInstance(14, 30, 0, 0); // 2:30 PMTime newTime = currentTime.addHours(3);System.debug(‘New Time (after adding hours): ‘ + newTime); // 17:30:00.000Z PM
3addMilliseconds(additionalMilliseconds): Adds the specified number of milliseconds to a Time.Time currentTime = Time.newInstance(14, 30, 0, 0); // 2:30 PMTime newTime = currentTime.addMilliseconds(5000);System.debug(‘New Time (after adding milliseconds): ‘ + newTime); // 14:30:05.000Z PM
4addMinutes(additionalMinutes): Adds the specified number of minutes to a Time.Time currentTime = Time.newInstance(14, 30, 0, 0); // 2:30 PMTime newTime = currentTime.addMinutes(45);System.debug(‘New Time (after adding minutes): ‘ + newTime); // 3:15 PM
5addSeconds(additionalSeconds): Adds the specified number of seconds to a Time.Time currentTime = Time.newInstance(14, 30, 0, 0); // 2:30 PMTime newTime = currentTime.addSeconds(30);System.debug(‘New Time (after adding seconds): ‘ + newTime); // 2:30:30 PM
6hour(): Returns the hour component of a Time.Time currentTime = Time.newInstance(14, 30, 0, 0); // 2:30 PMInteger hour = currentTime.hour();System.debug(‘Hour: ‘ + hour); // 14
7millisecond(): Returns the millisecond component of a Time.Time currentTime = Time.newInstance(14, 30, 0, 500); // 2:30:00.500 PMInteger millisecond = currentTime.millisecond();System.debug(‘Millisecond: ‘ + millisecond); // 500
8minute(): Returns the minute component of a Time.Time currentTime = Time.newInstance(14, 30, 0, 0); // 2:30 PMInteger minute = currentTime.minute();System.debug(‘Minute: ‘ + minute); // 30
9second(): Returns the second component of a Time.Time currentTime = Time.newInstance(14, 30, 45, 0); // 2:30:45 PMInteger second = currentTime.second();System.debug(‘Second: ‘ + second); // 45

What is DateTime Data Type in Apex?

A value that indicates a particular day and time, such as a timestamp will be considered under the DateTime Data Type in Apex. Always create DateTime values with a system static method.

You can add or subtract an Integer or Double value from a Datetime value, returning a DateTime value as a result. You can’t perform arithmetic operations that include two or more Datetime values. Instead, you can use the Datetime methods.

Datetime Methods

The following are methods for Datetime.

S.NoFunctionExample
1newInstance(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.Datetime dt = Datetime.newInstance(2024, 5, 16);System.debug(‘Datetime: ‘ + dt);
2newInstance(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.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);System.debug(‘Datetime: ‘ + dt);
3newInstance(date, time): Constructs a DateTime from the specified date and time in the local time zone.Date date = Date.newInstance(2024, 5, 16);Time time = Time.newInstance(12, 0, 0, 0);Datetime dt = Datetime.newInstance(date, time);System.debug(‘Datetime from date and time: ‘ + dt);
4newInstanceGmt(year, month, date): Constructs a Datetime from Integer representations of the specified year, month (1=Jan), and day at midnight in the GMT time zoneDatetime dt = Datetime.newInstanceGmt(2024, 5, 16);System.debug(‘Datetime GMT: ‘ + dt);
5newInstanceGmt(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.Datetime dt = Datetime.newInstanceGmt(2024, 5, 16, 12, 0, 0);System.debug(‘Datetime GMT: ‘ + dt);
6now(): Returns the current Datetime based on a GMT calendar.Datetime now = Datetime.now();System.debug(‘Current Datetime: ‘ + now);
7addDays(additionalDays): Adds the specified number of days to a Datetime.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Datetime newDt = dt.addDays(10);System.debug(‘Datetime after adding 10 days: ‘ + newDt);
8addHours(additionalHours): Adds the specified number of hours to a Datetime.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Datetime newDt = dt.addHours(5);System.debug(‘Datetime after adding 5 hours: ‘ + newDt)
9addMinutes(additionalMinutes): Adds the specified number of minutes to a Datetime.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Datetime newDt = dt.addMinutes(30);System.debug(‘Datetime after adding 30 minutes: ‘ + newDt);
10addMonths(additionalMonths): Adds the specified number of months to a Datetime.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Datetime newDt = dt.addMonths(2);System.debug(‘Datetime after adding 2 months: ‘ + newDt);
11addSeconds(additionalSeconds): Adds the specified number of seconds to a Datetime.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Datetime newDt = dt.addSeconds(45);System.debug(‘Datetime after adding 45 seconds: ‘ + newDt);
12addYears(additionalYears): Adds the specified number of years to a Datetime.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Datetime newDt = dt.addYears(1);System.debug(‘Datetime after adding 1 year: ‘ + newDt);
13date(): Returns the Date component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Date dateComponent = dt.date();System.debug(‘Date component: ‘ + dateComponent);
14dateGMT(): Return the Date component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Date dateGmtComponent = dt.dateGMT();System.debug(‘GMT Date component: ‘ + dateGmtComponent);
15day(): Returns the day-of-month component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer dayOfMonth = dt.day();System.debug(‘Day of month: ‘ + dayOfMonth);
16dayGmt(): Returns the day-of-month component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer dayOfMonthGmt = dt.dayGmt();System.debug(‘Day of month GMT: ‘ + dayOfMonthGmt);
17dayOfYear(): Returns the day-of-year component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer dayOfYear = dt.dayOfYear();System.debug(‘Day of year: ‘ + dayOfYear);
18dayOfYearGmt(): Returns the day-of-year component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer dayOfYearGmt = dt.dayOfYearGmt();System.debug(‘Day of year GMT: ‘ + dayOfYearGmt);
19format(): 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.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);String formattedDate = dt.format();System.debug(‘Formatted date: ‘ + formattedDate);
20format(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.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);String formattedDate = dt.format(‘yyyy-MM-dd HH:mm:ss’);System.debug(‘Formatted date: ‘ + formattedDate);
21format(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.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);String formattedDate = dt.format(‘yyyy-MM-dd HH:mm:ss’, ‘GMT’);System.debug(‘Formatted date GMT: ‘ + formattedDate);
22formatGmt(dateFormatString): Returns a Datetime as a string using the supplied Java simple date format and the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);String formattedDateGmt = dt.formatGmt(‘yyyy-MM-dd HH:mm:ss’);System.debug(‘Formatted date GMT: ‘ + formattedDateGmt);
23formatLong(): Converts the date to the local time zone and returns the converted date in long date format.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);String longFormattedDate = dt.formatLong();System.debug(‘Long formatted date: ‘ + longFormattedDate);
24getTime(): Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Long timeInMillis = dt.getTime();System.debug(‘Milliseconds since epoch: ‘ + timeInMillis);
25hour(): Returns the hour component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer hourComponent = dt.hour();System.debug(‘Hour: ‘ + hourComponent);
26hourGmt(): Returns the hour component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer hourComponentGmt = dt.hourGmt();System.debug(‘Hour GMT: ‘ + hourComponentGmt);
27isSameDay(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.Datetime dt1 = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Datetime dt2 = Datetime.newInstance(2024, 5, 16, 18, 0, 0);Boolean sameDay = dt1.isSameDay(dt2);System.debug(‘Is same day: ‘ + sameDay); // true
28millisecond(): Return the millisecond component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0, 500);Integer millisecondComponent = dt.millisecond();System.debug(‘Millisecond: ‘ + millisecondComponent);
29millisecondGmt(): Return the millisecond component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0, 500);Integer millisecondComponentGmt = dt.millisecondGmt();System.debug(‘Millisecond GMT: ‘ + millisecondComponentGmt)
30minute(): Returns the minute component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 30, 0);Integer minuteComponent = dt.minute();System.debug(‘Minute: ‘ + minuteComponent);
31minuteGmt(): Returns the minute component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 30, 0);Integer minuteComponentGmt = dt.minuteGmt();System.debug(‘Minute GMT: ‘ + minuteComponentGmt);
32month(): Returns the month component of a Datetime in the local time zone of the context user (1=Jan).Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer monthComponent = dt.month();System.debug(‘Month: ‘ + monthComponent); // 5
33monthGmt(): Returns the month component of a Datetime in the GMT time zone (1=Jan).Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer monthComponentGmt = dt.monthGmt();System.debug(‘Month GMT: ‘ + monthComponentGmt); // 5
34newInstance(milliseconds): Constructs a Datetime and initializes it to represent the specified number of milliseconds since January 1, 1970, 00:00:00 GMT.Long milliseconds = 1655404800000L; // Represents some specific date and timeDatetime dt = Datetime.newInstance(milliseconds);System.debug(‘Datetime from milliseconds: ‘ + dt);
35second(): Returns the second component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 45);Integer secondComponent = dt.second();System.debug(‘Second: ‘ + secondComponent);
36secondGmt(): Returns the second component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 45);Integer secondComponentGmt = dt.secondGmt();System.debug(‘Second GMT: ‘ + secondComponentGmt);
37time(): Returns the time component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Time timeComponent = dt.time();System.debug(‘Time component: ‘ + timeComponent);
38timeGmt(): Returns the time component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Time timeComponentGmt = dt.timeGmt();System.debug(‘Time component GMT: ‘ + timeComponentGmt);
39valueOf(dateTimeString): Returns a Datetime that contains the value of the specified string.Datetime dt = Datetime.valueOf(‘2024-05-16 12:00:00’);System.debug(‘Datetime from string: ‘ + dt);
40valueOfGmt(dateTimeString): Returns a Datetime that contains the value of the specified String.Datetime dt = Datetime.valueOfGmt(‘2024-05-16 12:00:00’);System.debug(‘Datetime GMT from string: ‘ + dt);
41year(): Returns the year component of a Datetime in the local time zone of the context user.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer yearComponent = dt.year();System.debug(‘Year: ‘ + yearComponent);
42yearGmt(): Returns the year component of a Datetime in the GMT time zone.Datetime dt = Datetime.newInstance(2024, 5, 16, 12, 0, 0);Integer yearComponentGmt = dt.yearGmt();System.debug(‘Year GMT: ‘ + yearComponentGmt);
salesforce-developer
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book A 15-Minutes Free Career Counselling Today!