0 votes
1 view
in Salesforce by (11.3k points)

Goal:

  1. I need to first create a String representing the current date.
  2. Afterwards this String needs to be parsed and used to build an instance of the Date class.

Initial attempt:

In my test class I create a current date as a String input for my tested method in the following manner:

String inputDate = date.today().format(); // 13:28:15:378 USER_DEBUG [24]|DEBUG|17.3.2017

However, when I attempt to create an instance of a Date object like this:

Date dateFromInput = date.valueOf(inputDate);

I receive the following error:

13:28:15:398 FATAL_ERROR System.TypeException: Invalid date: 17.3.2017

1 Answer

0 votes
by (28.6k points)

Date.format() will return a string in the current local date format of logged-in user.

Date.valueOf needs input string in format yyyy-MM-dd HH:mm:ss in the local time zone.

Below should work:

String inputDate = date.today().format('**yyyy-MM-dd HH:mm:ss**');

Date dateFromInput = date.parse(inputDate);

...