You need to sign in to do that
Don't have an account?
Suresh 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
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
Are you looking for something like this? or you need to change the format of the date??
You could use format DateTime methods.
Check the below code:
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.
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 should give you what you need;
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
right now i did the same but just different.
Thanks for the reply.
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);
}