1

I was testing Date.parse(str) method with following types of inputs:

  • String str1 = '06/06/2016'
  • String str2 = '06-06-2016'
  • String str3 = '2016-06-06'
  • String str4 = 'July 6th, 2016'
  • String str5 = '06, 06, 2016'
  • String str6 = '06 July, 2016'

Out of all above inputs, str1 is the only valid input and the Date.parse method does not work for the other formats. Does the Date.parse method support only the format followed by str1? Or does parse have a bigger picture than this?

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
DJD
  • 314
  • 1
  • 2
  • 16

1 Answers1

3

The input will only accept one format, and that format is based on the running user's locale. Note from the documentation (emphasis mine):

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

You can see how to get the format accepted by the running user as provided by @cropredy on this question: Get dateformat based on Locale.

So yes, for any given user, there is only one locale format that will work. For example, if you changed your locale to 'de', then you would have to specify Date.parse('25.12.2016') and an input of '06/06/2016' would error. If your locale is 'es' than it would be '25/12/2016', and if your locale is en_US then you would use '12/25/2016'.

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420