0

I am creating the CSV using the batch code. Here is the code

Date testDate = 07/08/2020;

String monthOfTheDate = testDate.month()+'';

String dayOfTheDate = testDate.day()+'';

Integer monthSize = monthOfTheDate.length();

System.debug('Month size: '+monthSize );

Integer daySize = dayOfTheDate.length();

System.debug('Day size: '+daySize );

String formattedMonth ;

String formattedDay ;

if(monthSize == 1 || daySize == 1) {

     formattedMonth = '0' +monthOfTheDate; 

     formattedDay = '0' +dayOfTheDate;

     fileRow = fileRow +',"' +(formattedMonth + '-' + formattedDay + '-' + docDate.year())+'"'; 

}
else {

    fileRow = fileRow +',"' +(docDate.month() + '-' + docDate.day() + '-' + docDate.year())+'"';  
          
}

Actual result: 7/8/2020. Expected result: 07/08/2020

How I can achieve the expected result. Can anyone help me?

Thanks in Advance

1 Answer 1

0

If you want to format the Date, you can do it this way.

String dateStr = DateTime.newInstance(testDate.year(), testDate.month(), testDate.day()).format('MM-dd-YYYY');

You can change the format with whatever you want as MM-dd-YYYY or MM/dd/YYYY

You must log in to answer this question.

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