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');

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

|improve this answer|||||
  • 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 '16 at 6:26
string year = '1993';
string month = '4';
string day = '20';

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

Date myDate = date.valueOf(stringDate);

|improve this answer|||||

Made this change in your existing code

|improve this answer|||||

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.