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

1 Answer 1

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

You must log in to answer this question.

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