0

Can anyone help me with this?. How to extract the time from the date field and also the date format while creating the CSV file in the batch class. Please find the below code.

String fileRow = '';
fileRow = fileRow ',' +Date field

Actual CSV:

Date
2/3/2020 12:00:00 AM

Expected CSV:

Date
3-2-2020
3

I am assuming you have a DateTime field.

You can use format() method on DateTime field, but if you need to do formatting on a Date field, you can convert Date to DateTime and use again format() method on it.

Account acc = [SELECT Id, CreatedDate FROM Account LIMIT 1];
System.debug(acc.CreatedDate);
System.debug(acc.CreatedDate.format('dd-MM-yyyy'));
System.debug(acc.CreatedDate.format('HH:mm:ss'));

Result:

2019-12-30 08:13:39
30-12-2019
08:13:39
-1

//s = '9/27/20 12:00 AM';
    public void dateformat(string s){
        formattedstring = '';
        system.debug(s);//2020-09-24T09:14:00.000+0000    ---'9/27/20 12:00 AM'
        list<string> s2 = s.split(' ');
        string s3 = s2[0]+', '+s2[1]+' '+s2[2];
        list<string> s4 = s2[0].split('/');
        string s5 = s4[0]+'/'+s4[1]+'/'+s4[2];
        string c2;
        list<string>timeval=s2[1].split(':');
        if (s2[2].contains('AM')){
            c2=timeval[0]+':'+timeval[1]+':00';
        }
        if (s2[2].contains('PM')){
            integer hour=integer.valueof(timeval[0])+12;
            c2=string.valueof(hour)+':'+timeval[1]+':00';
        }
        formattedstring = '20'+s4[2]+'-'+s4[0]+'-'+s4[1]+' '+c2;
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.