Primitive Data Types in Apex

There are 5 major data types available in Apex:

  • Primitive (Integer, Double, Long, Date time, String, ID, or Boolean).
  • Collections (Lists, sets, and maps).
  • sObject.
  • Enums.
  • Classes, Objects, and Interfaces.

Primitive Data Types

DatatypeDescription
IntegerA 32-bit number that doesnt include a decimal point. Integer num1=15; Integer num2=55;
LongA 64-bit number that doesnt include a decimal point. Long num3=547150L;
DecimalA number that include decimal points is called decimal. Decimal decNum=20.87;
DoubleA 64-bit number that include a decimal points is called double. Double d=8.14255;
BooleanA Boolean value can only be assigned true, false, or null. Boolean is Active=False.
DateDate value contain no information about time. A value that indicates a particular day is known as Date. E.g., 2019-11-13.
TimeThe time data type stores hours, minutes, seconds, and milliseconds.
DateTimeDatetime data type stores both dates and times.

Declaring Date Data Type

To declare a date variable, we have to define a date data type and the name of the variable.

The Date is a class in Salesforce. It is a predefined class.

Date Methods

The methods for the date are:

Method NamesUsage
day( )day( ) returns the day of the month component.
daysInMonth(year, month)daysInMonth(year, month) returns the number of days in a month of the specified year and a month.
format( ) 
isLeapYear(year)isLeapYear(year) returns true if the defined year is a leap year.

month( )month( ) returns the month component.
newInstance(year, month, day)newInstance(year, month, date) composes a date from Integer representations of the year, month, and day.
parse(stringDate)parse(stringDate) builds a date from a String. The format of the string depends upon the local date format.
today( )today( ) returns the current date according to the current users time zone.
valueof(stringDate)valueof(stringDate) returns a date that contains the value of the specified String.
valueof(fieldValue)valueof(fieldValue) converts the defined object to a date.  
year( )year( ) returns the year component.

Example:

Output:

Primitive Data Types in Apex

Example:

Printing todays date.

Output:

Primitive Data Types in Apex

Example:

Adding years in todays date.

Output:

Primitive Data Types in Apex

Example:

Adding months in todays date

Output:

Primitive Data Types in Apex

Example:

Adding days in today`s date.

Output:

Primitive Data Types in Apex

Example:

The below program is used to calculate no. of days between January 1 till December 31.

Output:

Primitive Data Types in Apex

Example:

Output:

Primitive Data Types in Apex

Time:

Time is a primitive data type.

Time Methods:

The methods for Time are:

MethodsUsage
addHours(additionalHours)addHours(additionalHours) sum the defined number of hours to time.
addMilliseconds(additionalMilliseconds)addMilliseconds(additionalMilliseconds) sum the defined number of milliseconds to a Time.
addMinutes(additionalMinutes)addMinutes(additionalMinutes) sum the defined number of minutes to a Time.
addSeconds(additionalSeconds)addSeconds(additionalSeconds) sum the defined number of seconds to a Time.
millisecond()millisecond() returns the millisecond component of a Time.
minute()minute() returns the minute component of a Time.
newInstance(hour, minutes, seconds, milliseconds)  newInstance(hour, minutes, seconds, milliseconds) build time from Integer representations of the defined hour, minutes, seconds, and milliseconds.  
hour()hour() returns the hour component of a Time.
second()second() returns the second component of a Time.

Example:

Output:

Primitive Data Types in Apex

Example:

Output:

Primitive Data Types in Apex

Example:

Output:

Primitive Data Types in Apex

Datetime:

Datetime constructs a Datetime from Integer representations of the defined year, month, day, hour, minute, and second in the local time zone. It will return datetime in GMT format.

Datetime Methods

MethodsUsage
addDays(additionalDays)This method adds the specified number of days to a given Datetime.
addHours(additionalHours)This method adds the specified number of hours to a Datetime.
addMinutes(additionalMinutes)This method adds the specified number of minutes to a Datetime.
addSeconds(additionalSeconds)This method adds the specified number of seconds to a Datetime.
addYears(additionalYears)This method adds the specified number of years to a Datetime.
date()This method returns the Date component of a Datetime in the local time zone of the context user.
dateGMT()This method returns Date component of the Datetime.
day()This method returns the day-of-month component of a Datetime.
dayGmt()This method returns the day-of-month component of a Datetime.
dayofYearGmt()This method returns the day-of-year component of a Datetime in the GMT time zone.
format()This method converts the date to the local time zone and returns the converted date as a formatted string by using the locale of the context user. And if the time zone cannot be determined, then GMT will be used.               
format(dateFormatString)This method converts to the local time zone and then returns the converted date as a string by using the Java simple date format.
formatLong()This method converts the date to the local time zone, and it returns the converted data in a long date format.
getTime()This method returns the number of milliseconds since January 1, 1970, 00:00:00.
hour()This method returns the hour component of a Datetime in the local time zone.
hourGmt()This method returns the hour component of a Datetime in the GMT time zone.
millisecond()This method returns the millisecond component of a Datetime in the local time zone.
millisecondGmt()This method returns the millisecond component of a Datetime in the GMT time zone.
minute()This method returns the minute component of a Datetime in the local time zone.
minuteGmt()This method returns the minute component of a Datetime in the GMT time.
month()This method returns the month component of a Datetime in the time zone of the context user (1=Jan).
newInstance(milliseconds)This method constructs Datetime and initializes it to represent the specified year, month, and day at midnight in the local time zone.
newInstance(date, time)This method constructs a Datetime from the defined date and time in the local time zone.
newInstance(year, month, day)This method constructs a Datetime from Integer representations of the defined year, month, and day at midnight in the local time zone.
newInstance(year, month, day, hour, minute, second)This method constructs a Datetime from Integer representations of the defined year, month, day, hour, minute, and second.
newInstanceGmt(date, time)This method constructs a Datetime from the specified date and time in the GMT time zone.
second()This method returns the second component of a Datetime in the local time zone.
secondGmt()This method returns the second component of a Datetime in the GMT time zone.
time()This method returns the time component of a Datetime in the local time zone.
timeGmt()This method returns the time component of a Datetime in the GMT time zone.
valueOf(dateTimeString)This method returns the time component of a Datetime in the GMT time zone.
valueOf(fieldValue)This method converts the specified object to a Datetime.
valueOfGmt(dateTimeString)This method returns a Datetime that contains the value of the specified String.
year()This method returns the year component of a Datetime in the local time zone.
yearGmt()This method returns the year component of a Datetime in the GMT time zone.

Example:

Output:

Primitive Data Types in Apex

Example:

Output:

Primitive Data Types in Apex

Example:

Output:

Primitive Data Types in Apex

Example:

Output:

Primitive Data Types in Apex

Example:

Output:

Primitive Data Types in Apex

Example:

Output:

Primitive Data Types in Apex

Pin It on Pinterest

Share This