How to convert string to Date in apex?

3.0K    Asked by ChrisDyer in Salesforce , Asked on Apr 24, 2021

What is wrong with this format while am upserting the date in an object?

Route__c r =new Route_c(); r.Plan_Date__c = date.parse(strDate); upsert r;

In the debug log, I get the error as |FATAL_ERROR|System.TypeException: Invalid date: Test123

Here Test123 is the different text to be inserted which accepts as Date and throws an error.

How to convert a string into apex date?


Answered by Crowny Hasegawa

This error is coming as system is not able to understand Test123 while parsing into date.

The format of the String depends on the local date format, like mm/dd/yyyy. If the parameter is not of this format, date.parse will throw error.

E.g.:

    r.Plan_Date__c = date.parse('12/27/2015');

You can also use date.valueOf(strDate) to parse string of format yyyy-MM-dd HH:mm:ss in the local time zone. You can use this link to read about more date properties



Your Answer

Answers (2)

In your case, the error you're encountering (System.TypeException: Sprunki Invalid date: Test123) indicates that the string you're trying to convert ("Test123") is not a valid date format. You need to ensure that the string is formatted correctly as a date.

1 Month

In Apex, Salesforce's proprietary programming language, you can convert a string to a date using the Date and DateTime classes. Here’s how you can do it:


Converting String to Date

If your string represents a date in a standard format (like YYYY-MM-DD), you can use the Date.valueOf method:

String dateString = '2024-05-29';
Date dateValue = Date.valueOf(dateString);
System.debug('Date: ' + dateValue);

Handling Different Date Formats

If your date string is in a different format, you will need to parse it accordingly. Apex does not have built-in methods for parsing non-standard date formats directly to a Date, so you might need to manipulate the string first.


Here’s an example of converting a date string in the format DD-MM-YYYY to a Date:

String dateString = '29-05-2024';
List dateParts = dateString.split('-');
Date dateValue = Date.newInstance(
    Integer.valueOf(dateParts[2]), // Year
    Integer.valueOf(dateParts[1]), // Month
    Integer.valueOf(dateParts[0]) // Day
);
System.debug('Date: ' + dateValue);

Converting String to DateTime

For converting a string to a DateTime, use the DateTime.valueOf method. The string must be in the format yyyy-MM-dd HH:mm:ss:

String dateTimeString = '2024-05-29 14:30:00';
DateTime dateTimeValue = DateTime.valueOf(dateTimeString);
System.debug('DateTime: ' + dateTimeValue);

Handling Different DateTime Formats

For non-standard DateTime strings, you can similarly manipulate the string before conversion. For example, converting a string in the format DD-MM-YYYY HH:mm:ss:

String dateTimeString = '29-05-2024 14:30:00';
List dateTimeParts = dateTimeString.split(' ');
List dateParts = dateTimeParts[0].split('-');
List timeParts = dateTimeParts[1].split(':');
DateTime dateTimeValue = DateTime.newInstance(
    Integer.valueOf(dateParts[2]), // Year
    Integer.valueOf(dateParts[1]), // Month
    Integer.valueOf(dateParts[0]), // Day
    Integer.valueOf(timeParts[0]), // Hour
    Integer.valueOf(timeParts[1]), // Minute
    Integer.valueOf(timeParts[2]) // Second
);
System.debug('DateTime: ' + dateTimeValue);

Summary

  • Use Date.valueOf for standard YYYY-MM-DD date strings.
  • For non-standard date formats, split the string and use Date.newInstance.
  • Use DateTime.valueOf for standard yyyy-MM-dd HH:mm:ss datetime strings.
  • For non-standard datetime formats, split the string and use DateTime.newInstance.

By following these methods, you can convert strings to Date and DateTime in Apex, accommodating both standard and custom formats.


5 Months

Interviews

Parent Categories