1

I have 3strings. I want to convert it into date

How to achieve this ?

string a='20';
string b='4';
string c='1993';

String dt = DateTime.newInstance(a,b,c).format('d-MM-YYYY');

3 Answers 3

3

Alternatively, the way you were headed it's something like this:

String dt = Date.newInstance(Integer.valueOf(c),Integer.valueOf(b),Integer.valueOf(a)).format();

Keep in mind:

  • The newInstance method takes Integers as parameters
  • format() will format the date in the current user's locale

Read more on Date Methods

1
  • 2
    Nice point. Since the OP doesn't ask for a String as the output, even .format() is not necessary here. And although it is not necessary in this case, I usually use .trim() before using Integer.valueOf() just for more safety.
    – Lance Shi
    Nov 28, 2016 at 6:26
1
string year = '1993';
string month = '4';
string day = '20';

string stringDate = year + '-' + month
 + '-' + day ;

Date myDate = date.valueOf(stringDate);

system.debug('mapPeriodDay>>>'+myDate);
0

Made this change in your existing code

DateTime.newInstance(integer.valueof(c),integer.valueof(b),integer.valueof(a)).format('dd-MM-YYYY');

You must log in to answer this question.

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