| by Arround The Web | No comments

Salesforce Apex – Date Format

Tracking the details of customers in Accounts/Contacts is very important in Customer Relationship Management (CRM). Based on the date, all the processes like purchasing, quoting, and servicing happen. Let’s look at how to create the date from string in Salesforce and convert the date to string. As part of this tutorial, we will also see the methods that are applied on the formatted date like adding days, years, months, etc., with examples.

Apex Date Class

Date class supports all date methods which are primitive. It uses the “System” namespace. We can use the valueOf() method from this class to convert the string to a date format. After formatting the date, we apply some methods to add the days, months, years, ect., to this formatted date using some methods that are also available in this date class.

First, we will see how to convert the string to date using the valueOf() method in Salesforce Apex.

Date.valueOf()

The valueOf() in Apex is available in the “Date” class which takes the string as an argument that converts the specified string into the date format. The input string (parameter) includes three attributes – year, month, and day. All these three are combined/concatenated and passed to the method.

Syntax: ValueOf()

As already mentioned, we need to declare a variable of the “Date” class and pass the string to it.

Date date_variable= date.valueOf(string_date_format);

Example:

Step 1:

Let’s have a string that holds the “Year-Month-Day”.

// Declare year,month and day one by one

String Year = '2023';

String Month = '4';

String Day = '5';

// Concatenate all of them into a string

String string_type = Year + '-' + Month + '-' + Day;

system.debug(string_type);

Output:

Step 2:

Now, convert the previous “Date” string to “Date” using the valueOf() method from the Apex “Date” class.

// Convert string-Date to Date

Date converted_date = date.valueOf(string_type);

system.debug(converted_date);

Output:

We can see that after converting to “Date”, it automatically returns the date in the DateTime format. It won’t consider the Time (Hours:Minutes:Seconds) even if you pass these to the valueOf() method. The returned format is YYYY-MM-DD HH:MM:SS.

Convert the Date to String

There are two ways to convert the “Date” to “String”. One way that is supported by Apex is using the format() method. The other way is manually extracting the “Date” attributes (year, month, and day) and concatenating them into string. Let’s look at them one by one.

1. Format()

The format() method is available in the “Date” class in Apex which converts the “Date” into the string in the “M/D/YYYY” format. We can also pass the specified format as a parameter to this method. The specified format is the default format.

Syntax:

input_date.format(“format_type”)

Example:

Let’s consider the previous example and convert back the “Date” to “String” using the format() method. We are not providing any specific format to the format() method.

// Declare year,month and day one by one

String Year = '2023';

String Month = '4';

String Day = '5';

// Concatenate all of them into a string

String string_type = Year + '-' + Month + '-' + Day;

// Convert string-Date to Date

Date converted_date = date.valueOf(string_type);

system.debug(converted_date);

// format()

system.debug(converted_date.format());

Output:

2. Using the Date Class Method

In this scenario, we extract the year, month, and day separately from the “Date” using the available methods in the Apex “Date” class. Next, we concatenate these three to return the “Date” in “String” format.

  1. day() – This method is used to extract the date from the “Date”. It returns the integer that specifies the date.
  2. month() – This method is used to extract the month from the “Date”. It returns the integer that specifies the month number. We can refer to January as 1, February as 2…. December as 12.
  3. year() – This method is used to extract the year from the “Date”. It returns the year in a four-digit format.

Syntax:

input_date.year() + '-' + input_date.month() + '-' + input_date.day()

Example:

Convert back the “Date” to “String” in the “YYYY-M-D” format.

// Declare year,month and day one by one

String Year = '2023';

String Month = '4';

String Day = '5';

// Concatenate all of them into a string

String string_type = Year + '-' + Month + '-' + Day;

// Convert string-Date to Date

Date converted_date = date.valueOf(string_type);

system.debug(converted_date);

// Convert Date to String

system.debug(converted_date.year() + '-' + converted_date.month() + '-' + converted_date.day());

Output:

Practical Examples:

Let’s see how to update the “Date” like adding days, years, and months to the existing date in the Salesforce objects. All three methods take the “n” integer as an argument.

1. addDays()

To add days to the existing date, Apex supports the addDays() method which is available in the “Date” class.

2. addMonths()

To add months to the existing date, Apex supports the addMonths() method which is available in the “Date” class.

3. addYears()

To add years to the existing date, Apex supports the addYears() method which is available in the “Date” class.

Syntax:

Let’s look at the syntax for these three methods. Here, “n” species the integer value.

  • input_Date.addDays(n)
  • input_Date.addMonths(n)
  • input_Date.addYears(n)

Before going to the example, consider the following:

  1. Login into Salesforce and go to the “App Launcher” and search for “Campaign” in the tab. Here, we insert the “Campaign Name” and “End Date”.

  1. Choose “Campaigns” and click on “New”.

  1. A popup appears to insert the data into the campaign. Add “Linux Hint Posts” under the “Campaign Name” and set the “End Date” as 4/5/2023. Then, click on “Save”.

We are ready with the record. (Type – Conference and Status – Planned will come automatically. Leave that). Let’s open the anonymous window in the developer console.

Example 1:

Let’s use the addDays() method to add 10 days to the existing “End Date” in the “Campaign” object.

  1. To do this, we first need to use the SOQL Query to get the record from the “Campaign” object and store this record in a “List” object.
  2. Next, we use the “for” loop to iterate the list and use the addDays() method to add 10 days to the “End Date”.
  3. Finally, we use the “update DML” statement to update the “End Date” in the “Campaign” object.
// Return Name and EndDate from the Campaign using SOQL

List<Campaign> query1 = [SELECT Name,EndDate FROM Campaign WHERE Name = 'Linux Hint Posts'];

// Add 10 days to the EndDate using addDays() method

for(Campaign i: query1){

  i.EndDate = i.EndDate.addDays(10);

}

// Use update DML to update the EndDate

update query1;

system.debug(query1);

Output:

The previous “End Date” is April 5th. After adding 10 days to it, the “End Date” is now April 15th.

We can also check in the “Campaign” tab. Move back and refresh the page.  You can see that the “End Date” is updated.

Example 2:

Let’s use the addMonths() method to add 3 months to the existing “End Date”.

// Return Name and EndDate from the Campaign using SOQL

List<Campaign> query1 = [SELECT Name,EndDate FROM Campaign WHERE Name = 'Linux Hint Posts'];

system.debug('Actual: '+query1);

// Add 3 months to the EndDate using addMonths() method

for(Campaign i: query1){

  i.EndDate = i.EndDate.addMonths(3);

}

// Use update DML to update the EndDate

update query1;

system.debug('Updated: '+query1);

Output:

The previous month in the “End Date” is April. After adding 3 months, it is now July.

Example 3:

Let’s use the addYears() method to add 3 years to the existing “End Date”.

// Return Name and EndDate from the Campaign using SOQL

List<Campaign> query1 = [SELECT Name,EndDate FROM Campaign WHERE Name = 'Linux Hint Posts'];

system.debug('Actual: '+query1);

// Add 3 years to the EndDate using addYears() method

for(Campaign i: query1){

  i.EndDate = i.EndDate.addYears(3);

}

// Use update DML to update the EndDate

update query1;

system.debug('Updated: '+query1);

Output:

After adding 3 years to the “End Date”, the updated year is 2026.

Conclusion

Formatting the date in Salesforce Apex is quite simple. As part of this tutorial, we learned how to convert the “Date” from “String” format to “Date” using the valueOf() method that is available in Apex “Date” Class. If you want to convert back the date to string, we use the format() and date attributes like day(), month() and year(). Finally, we ended this guide by discussing the DML operation on the Salesforce “Campaign” object to update the “End Date” using addDays(), addMonths(), and addYears() methods with separate examples.

Share Button

Source: linuxhint.com

Leave a Reply