Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k 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 (32.1k points)
edited by

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);

 Looking for a comprehensive Salesforce course? Enroll now!

Browse Categories

...