0

There seems to be some parsing limitation on string dates in apex.

When I am doing the following it throws a system exception saying invalid date:

String s = '12/31/5000';
Date.parse(s);

However the following works:

String s = '12/31/3000';
Date.parse(s);

The break seems to happen at year 4000. Parsing of Dates after 12/31/4000 are not working while ones before 12/31/4000(inclusive) are working.

Is this a check on org level(because I tried on multiple orgs) or is this a generic salesforce issue.

5
  • what you are trying to achieve with year>4000? Commented Jul 5, 2018 at 13:00
  • I think you mean String s = .... instead of Date s = ...., right? Commented Jul 5, 2018 at 13:02
  • @Santanu There is a business logic to check if the text field(holding string date) is above a certain threshold date. Data is being pumped from external system here.
    – starhunter
    Commented Jul 5, 2018 at 13:14
  • @rael_kid Yes. Corrected.
    – starhunter
    Commented Jul 5, 2018 at 13:15
  • I think this simply could have been found by google search or even some minor research in the documentation. help.salesforce.com/articleView?id=000240801&type=1 this article in the documentation was the very first from a basic google search
    – Ronnie
    Commented Jul 7, 2018 at 2:28

1 Answer 1

7

Yes, there is a limitation. It is documented on https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm at the bottom of the page

Minimum and Maximum Dates

Only dates within a certain range are valid. The earliest valid date is 1700-01-01T00:00:00Z GMT, or just after midnight on January 1, 1700. The latest valid date is 4000-12-31T00:00:00Z GMT, or just after midnight on December 31, 4000. These values are offset by your time zone. For example, in the Pacific time zone, the earliest valid date is 1699-12-31T16:00:00, or 4:00 PM on December 31, 1699.

4
  • Weirdly enough, executing the following code will not give an error for me: DateTime dt = DateTime.newInstance(4010, 12, 31, 0, 0, 0); Commented Jul 5, 2018 at 13:01
  • @rael_kid Are you sure that you aren't in the UTC +87648 timezone?
    – Derek F
    Commented Jul 5, 2018 at 13:04
  • @rael_kid It seems to be a feature of DateTime.newInstance. However, if you try to store that date in the database, you'll get an invalidate date error, as you'd expect.
    – sfdcfox
    Commented Jul 5, 2018 at 13:19
  • Yeah, I would expect that error when saving the date. Funny that you can use them in code though... @DerekF; yeah, I'm secretly living in the year 2028 :P Commented Jul 5, 2018 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .