In a recent Salesforce development task, I worked on a Salesforce application that required me to collect customer information through a web form. One of the fields in this form is the customer’s preferred appointment date, which is submitted as a string.
I had to convert the string to the date data type to save this date to the Salesforce date field. In this Salesforce tutorial, I will explain how to convert a string to date in Salesforce Apex.
Convert String to Date in Salesforce Apex
To learn how to convert a string to a date in Salesforce Apex, navigate to the Salesforce developer console and follow the steps below.
- To create an Apex class, click on the File in the nav bar and select New > Apex Class.
- Enter the new class’s name and click the OK button.
- Enter the code below in the developer console.
public class ConvertStringtoDate {
public static Date convertStringToDate(String dateString) {
return Date.valueOf(dateString);
}
public static void testConversion() {
String validDateString = '2024-09-05';
Date validDate = convertStringToDate(validDateString);
System.debug('Valid Date: ' + validDate);
}
}Now, I will break down the above code for better understanding.
- In the public class ConvertStringtoDate, I defined a static method as convertStringToDate.
- The method’s return type is Date, a built-in Salesforce field type representing a date without time.
- The function “valueOf(dateString)” converts the date string, which should be in the format YYYY-MM-DD, into a Date object.
- We have used convertStringtoDate with validDateString as the argument, converting it into a Date data type and storing it as a valid date.
- After entering and saving the code, execute it in the anonymous window with ” Ctrl+E.”
In the anonymous window, enter the “Classname.testconversion();” then select the Open the log checkbox and click the Execute button.

- In the debug console, select the checkbox to debug only, and there you will see the date string converted to a date and displayed as a Valid date.

This way, we can convert the date string to a date data type using the ‘valueOf(dateString);‘ function.
Handle Exceptions and Errors While Converting String to Date in Apex
There is a fair chance of getting errors while converting the string to a date in Salesforce Apex, and the reason might be an incorrect date format. For example, if the valid date format is YYYY-MM-DD, and you are trying to convert a date to DD-MM-YYYY format, you will get an error.
It is always best practice to use try and exception methods so that this one error does not affect the rest of the code.
In the steps below, I will explain how to handle invalid date formats and errors in our Apex code. To do so, navigate to the Salesforce developer console and follow the steps below.
- To create an Apex class, click on the File in the nav bar and select New > Apex Class.
- Enter the new class’s name and click the OK button.
- Enter the code below in the developer console.
public class ConvertStringtoDate {
public static Date convertStringToDate(String dateString) {
Date resultDate;
try {
resultDate = Date.valueOf(dateString);
} catch (Exception e) {
System.debug('Error converting string to date: ' + e.getMessage());
resultDate = null;
}
return resultDate;
}
public static void testConversion() {
String validDateString = '2024-09-05';
String invalidDateString = '2024-02-30';
Date validDate = convertStringToDate(validDateString);
System.debug('Valid Date: ' + validDate);
Date invalidDate = convertStringToDate(invalidDateString);
System.debug('Invalid Date: ' + invalidDate);
}
}Below is the explanation of the code structure we have used to handle errors while converting a string to a date in Apex.
- The try block Date.valueOf(dateString) attempts to convert the ‘datestring‘ to a date object. The string should be in the format yyyy-mm-dd (eg. 2024-07-04)
- The catch block will check if the string conversion fails and an exception is caught. If so, the error message will be displayed in the console log, showing the result date as null.
- In the testConversion method, we will display two date fields, valid and invalid, and both of them will be displayed in the console log.
- The ‘testconversion‘ method shows the usage of convertStringToDate with both valid and invalid date strings and logs the results for verification.
- After this, press “Ctrl+E” to execute the code.
- In the anonymous window, enter the code below, select the checkbox, open the log, and click the Execute button.
ConvertStringtoDate.testConversion();Here, “ConvertStringtoDate” is the class name, and “testConversion” is the name of the conversion method.

- In the console log window, click on the checkbox debug only; then, in the execution log, you will see the valid date, and for the invalid date, you will get the error message displayed in the log.

This way, you can handle exceptions and errors while converting a string to a date in Apex using the try-catch method and exception function.
Convert String To Date in the Format dd-mm-yyyy in Salesforce Apex
When you have to change the date format according to your local format, there is an approach that I’ll explain in the steps below.
For example, the string you converted to the date is in the format yyyy-mm-dd, but in your local system, the format is dd-mm-yyyy. In that situation, you have to change the format.
In the steps below, I will show you how you can change the format of string-to-date conversion in Salesforce Apex.
- Navigate to the Salesforce developer console, and to create a new Apex class, select File > New > Class.
- Enter the Class Name and click the OK button.
- In the developer console, enter the code below and save it. In the code below, we used the datetime function because Apex does not allow the direct format of a date.
public class StringDateFormat {
public static Date convertStringToDate(String dateString) {
return Date.valueOf(dateString);
}
public static void testConversion() {
String validDateString = '2024-09-09';
Date validDate = convertStringToDate(validDateString);
Date d = Date.today();
DateTime dateTimeValue = DateTime.newInstance(d.year(), d.month(), d.day());
String formattedDate = dateTimeValue.format('dd-MM-yyyy');
System.debug('Formatted Date: ' + formattedDate);
}
}Let’s understand the functions and methods used in the above code to change the date format.
- We have used the DateTime function for formatting; since Date in Apex does not have a format method that accepts a custom pattern, we convert the Date object to a DateTime object using DateTime.newInstance().
- The dateTimeValue.format() will change the datetime to the desired pattern.
- After saving the code, if there are no errors (problems), execute the code in the anonymous window using “Ctrl+E.”
- Enter the code as [ClassName.Methodname(); ] In the anonymous window, select the checkbox, open the log, and click Execute.

- In the console log, select the checkbox to debug only. Then, in the debug, we will see that the date format is displayed as “dd-mm-yyyy” format.

Following the above steps allows us to change the date format for String to Date conversion in Salesforce Apex.
Conclusion
In this Salesforce tutorial, we have learned to convert data from a string to a date data type and handle errors and exceptions while converting the string date to a date in Salesforce Apex using the try-and-catch method.
Finally, we have also learned to change the date format of a string in Salesforce Apex by using the dateTimeValue. format () function. By now, you will be able to convert a date from a string to a date object in your Salesforce database.
You may also like to read:
- Convert a String to an Integer using Apex in Salesforce
- Get Current Datetime in Apex Salesforce
- Convert String to ID in Salesforce Apex
- Convert Integer Data Type to String in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.