function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vidya DVidya D 

Convert String to Date - Exception in US locale when DateString has -

 

 

 

I am trying to parse Date string to Date data type


While executing Anonymous code below 
Date Birthdate = date.parse('09-05-1954');
System.debug('Birthdate '+ Birthdate); 

throws exception
6:11:11.041 (41552000)|EXCEPTION_THROWN|[1]|System.TypeException: Invalid date: 09-05-1954
16:11:11.041 (41707000)|FATAL_ERROR|System.TypeException: Invalid date: 09-05-1954

But Date String with Slash works fine
Date Birthdate = date.parse('09/05/1954');
System.debug('Birthdate '+ Birthdate); 

Output

16:15:26.160 (160415000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
16:15:26.161 (161357000)|USER_DEBUG|[2]|DEBUG|Birthdate 1954-09-05 00:00:00

This happens only for US locale.

 

Any idea - how to solve this?

sambasamba

 

The System date format is current user's Time Zone date format. So All of dates format is the same. then Date Birthdate = date.parse('09-05-1954'); If this value is from page, then its format should be system date format:09/05/1954.

 

Hope this helps.

 

Thanks,

Samba

crop1645crop1645

Yes, parsing dates entered from external systems is a pain in Salesforce as the SFDC date parsing is limited. I use this Utility class method to parse dates in locale or yyyy-mm-dd format and it could be extended for other allowed formats

 

	//	--------------------------------------------------------
	//	parseDate; null is invalid Date; yyyy-mm-dd and locale-specific e.g. mm/dd/yyyy or dd/mm/yyyy formats supported
	//	--------------------------------------------------------
	public static Date parseDate(String inDate) {
		Date	dateRes		= null;
		//	1 - Try locale specific mm/dd/yyyy or dd/mm/yyyy	
		try {
			String candDate		= inDate.substring(0,Math.min(10,inDate.length()));// grab date portion only m[m]/d[d]/yyyy , ignore time
			dateRes 	= Date.parse(candDate);
		}
		catch (Exception e) {}

		if (dateRes == null) {
		//	2 - Try yyyy-mm-dd			
			try {
				String candDate		= inDate.substring(0,10);			// grab date portion only, ignore time, if any
				dateRes				= Date.valueOf(candDate);
			}
			catch (Exception e) {} 
		}
		
		return dateRes;
	}

	@isTest
	private static void testParseDate() {
		System.assertEquals(Date.newInstance(2020,1,1),		parseDate('2020-01-01'));
		System.assertEquals(Date.newInstance(2020,1,1),		parseDate('2020-01-01T01:09:00Z'));
		System.assertEquals(Date.newInstance(2020,1,1),		parseDate('01/01/2020'));
		System.assertEquals(Date.newInstance(2020,1,1),		parseDate('1/1/2020'));
		System.assertEquals(Date.newInstance(2020,1,1),		parseDate('01/01/2020 05:08:00.000-0800'));
		System.assertEquals(null,							parseDate(null));
		System.assertEquals(null,							parseDate(''));
		System.assertEquals(null,							parseDate('ab/de/1201'));
		System.assertEquals(null,							parseDate('13/01/2020'));
		System.assertEquals(null,							parseDate('2020-13-01'));
				
	}

 

Note that if you need to parse dates from a VF page, then I strongly recommend changing the VF apex:inputText component to apex:inputField and associate that inputField to an SObject field, even to the extent of creating new SObject fields of type Date if necessary.  In this way, you will always get a valid date from the user input and you won't care about these locale issues

 

 

 

 

 

 

 

chandrasekhar saladi 6chandrasekhar saladi 6
Date Birthdate = date.parse('1954/09/05');
Try giving date in the format yyyy-mm-dd. It should work.
Navin Dayama 2Navin Dayama 2

Hey there,

parse(stringDate)
Constructs a Date from a String. The format of the String depends on the local date format.

So, try to put date of your org date type like Date d= date.parse('09/05/2000'); || Date d= date.parse('2000/09/05'); || 

Mark best answer if its work.