function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Suresh RaghuramSuresh Raghuram 

convert a string of datetime into date in apex class

11/28/14 06:00 AM is the string i want only date from it any help is appriciable
Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
string Dt = '11/28/14 06:00 AM';

String[] str = dt.split(' ');
String[] dts = str[0].split('/');

Date myDate = date.newinstance(Integer.valueOf('20'+dts[2]), Integer.valueOf(dts[0]), Integer.valueOf(dts[1]));

System.debug('--------------------------'+myDate);

 I could not find a quick solution, but i guess this might work out for you? let me know if it doesnt...

 

gud luck.

All Answers

Naidu PothiniNaidu Pothini
string Dt = '11/28/14 06:00 AM';
String[] str = dt.split(' ');
System.debug('--------------Date------------'+str[0]);

 Are you looking for something like this? or you need to change the format of the date??

Rahul SharmaRahul Sharma

You could use format DateTime methods.

Check the below code:

Datetime myDateTime = DateTime.parse('10/14/2011 11:46 AM');
String dateInStringString = myDateTime.format('MM/dd/yyyy');
system.debug('##dateInStringString - '+dateInStringString);

 

 

Suresh RaghuramSuresh Raghuram

Hi Naidu,

 

Thanks for the quick response.

i did as you said and getting yy, mm,dd. from the source. but my target is yyyy,mm,dd.

 

i tried few date format methods but not get thorough.

 

Any ideas.

 

Thanks in advance.

Naidu PothiniNaidu Pothini
string Dt = '11/28/14 06:00 AM';

String[] str = dt.split(' ');
String[] dts = str[0].split('/');

Date myDate = date.newinstance(Integer.valueOf('20'+dts[2]), Integer.valueOf(dts[0]), Integer.valueOf(dts[1]));

System.debug('--------------------------'+myDate);

 I could not find a quick solution, but i guess this might work out for you? let me know if it doesnt...

 

gud luck.

This was selected as the best answer
bouscalbouscal

This should give you what you need;

 

String srcString='11/28/14 06:00 AM';

String mm=srcString.subString(0,2);
String dd=srcString.subString(3,5);
String yy=srcString.subString(6,8);

myDate=Date.newInstance(integer.valueOf(yy), 
     integer.valueOf(mm), 
     integer.valueOf(dd));

 

Suresh RaghuramSuresh Raghuram

ya rahul implementing the same, but my problem is

 

Source date year is of 2 digits i mean it is not 2011 it is 11.

 

In the target it is accepting as 4 digits like 2011.

 

there fore i am getting the invalid data error

Suresh RaghuramSuresh Raghuram

right now i did the same but just different.

 

Thanks for the reply.

Ramakrishnan AyyanarRamakrishnan Ayyanar
try this it's both 24 and 12 hours

string Dt = '11/28/2014 10:10';

String[] str = dt.split(' ');
String[] newdates = str[0].split('/');
String[] newTimes = str[1].split(':');

if(str.size()>2)
{
String newampm = str[2];
System.debug('------------newampm--------------'+newampm);

String newmydate=Integer.valueOf(newdates[0])+'/'+Integer.valueOf(newdates[1])+'/'+Integer.valueOf(newdates[2])+' '+Integer.valueOf(newTimes[0])+':'+Integer.valueOf(newTimes[1])+' '+newampm;
//DateTime myDate = Datetime.parse(newmydate);
DateTime myDate =datetime.newInstance(Integer.valueOf(newdates[2]), Integer.valueOf(newdates[0]), Integer.valueOf(newdates[1]), Integer.valueOf(newTimes[0]), Integer.valueOf(newTimes[1]), Integer.valueOf(0)); //Datetime.parse(newmydate);

System.debug('--------------------------'+myDate);

}
else
{

    string newampm;
    integer newhour;
    if(integer.valueOf(newTimes[0])>integer.valueOf('12'))
    {
        newhour=integer.valueOf(newTimes[0])-integer.valueOf('12');
        newampm='PM';
    }
    else
    {
         newhour=integer.valueOf(newTimes[0]);
         newampm='AM';
    }
String newmydate=Integer.valueOf(newdates[0])+'/'+Integer.valueOf(newdates[1])+'/'+Integer.valueOf(newdates[2])+' '+Integer.valueOf(newhour)+':'+Integer.valueOf(newTimes[1])+' '+newampm;
DateTime myDate =datetime.newInstance(Integer.valueOf(newdates[2]), Integer.valueOf(newdates[0]), Integer.valueOf(newdates[1]), Integer.valueOf(newhour), Integer.valueOf(newTimes[1]), Integer.valueOf(0)); //Datetime.parse(newmydate);
System.debug('--------------------------'+myDate);
 
}