2

If I changed the time zone in Salesforce Org then getting the 'Invalid date Error'. It works fine if I select the Indian time zone but If I select other than current Timezone then Displaying error while parsing the date.

The Code is like this:

String dayOrDate = '2015-06-04';

List <String> dateParts = dayOrDate.split('-');

Date tempDate = date.parse(dateParts[1]+'/'+dateParts[2]+'/'+dateParts[0]);

Datetime dateWithTime = datetime.newInstance(tempDate.year(), tempDate.month(),tempDate.day());

dayOrDate = dateWithTime.format('dd- MMM-yy');

Please Can anyone please point me to solution ? Thanks in Advance!!

4
  • Can you tell from where you are changing time zone. I replicated your scenario in my org. First I used "(GMT+05:30) India Standard Time (Asia/Kolkata)" and then changed to "(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)". In both cases it showed same output "04- Jun-15"
    – Nebula
    Commented Jun 5, 2015 at 7:38
  • Thanks for reply.....In your Personal setting change the Time Zone & Locale, then try..Please let me know the result..
    – Shilpa
    Commented Jun 5, 2015 at 8:54
  • I did the same and got no error, may m missing something. Are you asking to change time zone from company information or from user's personal information. I ran the scenario by changing user's personal information.
    – Nebula
    Commented Jun 5, 2015 at 9:45
  • changing the time Zone and locales from personal information setting will give an error while parsing the date. So Now I have removed the parsing & just displayed in one format .& it's working...Thanks for your response!
    – Shilpa
    Commented Jun 8, 2015 at 9:36

1 Answer 1

3

You are using date.parse on a string that you construct as centre number / right number / left number

So in your example you turn 2015-06-04 into 04/06/2015. I would understand this as the 4th of June but our american friends would (wrongly) think that this is 6th of April.

When you call date.parse it uses the users settings to decide whether it should be mm/dd/yyyy or dd/mm/yyyy

So for some dates and users it will fail because it mixes month and day.

String dayOrDate = '2015-06-30';
List<String> dateParts = dayOrDate.split('-');
Datetime dateWithTime = Datetime.newInstance(Integer.valueOf(dateParts[0]), Integer.valueOf(dateParts[1]), Integer.valueOf(dateParts[2]));
dayOrDate = dateWithTime.format('dd- MMM-yy');
System.debug(dayOrDate);

returns

30- Jun-15

9
  • So Is there any standard way to recognize the date format and that will parse automatically?
    – Shilpa
    Commented Jun 5, 2015 at 8:57
  • Date.parse respects the users locale. So if the date is coming from user entry then you can use that. If it's coming from a system and you know which format the system is using, you can work with that.
    – Doug B
    Commented Jun 5, 2015 at 8:59
  • Actually we have used this in app exchange product So the date will come from system and the system can be in any country with different format. So how do i handle this scenario? I can not give a test condition as country specific. Please reply..
    – Shilpa
    Commented Jun 5, 2015 at 9:05
  • If the date is coming from the system (i.e. Salesforce) then take the date as a Date rather than a string.
    – Doug B
    Commented Jun 5, 2015 at 9:09
  • But to parse we need to pass it as a string parameter. see this link : developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Shilpa
    Commented Jun 5, 2015 at 9:22

You must log in to answer this question.

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