I have date format something like this. I don't know how to format this and this value needs to used on a close date in Opportunities. Please help me to format this. I tried below but throwing errors like invalid date format.

String data = '20/12/2020  00:00:00';
  • Presume you mean parse - go from a string to an Apex date object?
    – Keith C
    Commented Jan 15, 2021 at 16:56

2 Answers 2


Apex doesn't have pattern-based date parsing as many other languages do. So for an unsupported format, and to be sure the code will work whatever locale the user has set, you need to write a few lines of code such as this customParse method:

private class CustomParseTest {

    private static Date customParse(String s) {
        String[] dt = s.split('  ');
        String[] parts = dt[0].split('/');
        return Date.newInstance(

    static void test() {
        String s = '20/12/2020  00:00:00';
        Date d = customParse(s);
        System.assertEquals(Date.newInstance(2020, 12, 20), d);

You have to remove the unexpected time part :

String data = '20/12/2020 00:00:00';

1.Remove the time part with substring

String stringDate = data.substring(0,10);

output : DEBUG | stringDate = 20/12/2020

2.Convert the string to date with Date.parse function

Date dt = Date.parse(stringDate);

You must log in to answer this question.

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