I want to extract time from time picklist(12 PM,12 AM,4PM ,6:15 PM are picklist values) in apex like
12 PM as 12:00:00
12 AM as 00:00:00
4 PM as 16:00:00
6:15 PM as 18:15:00
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityUse Datetime.parse(String datetime)
It works like
Datetime dt = DateTime.parse('10/14/2011 11:46 AM');
Now you have just a time and there is no such function on the Time object. As a workaround use a fake date and extract the time component with DateTime.time()
Time t = DateTime.parse('10/14/2011 11:46 AM').time();
In your case
Time t = DateTime.parse('10/14/2011 ' + YouObject__c.YourPicklist__c).time();
dev,
Here is a method you could use to convert those picklist values into a time string. You can tweak as you like if you actually want to get a Time out of it.
public string convertTime(string t)
{
//Add the minutes if they are not provided
if(!t.contains(':'))
{
t = t.replace(' ','');
t = t.replace('PM',':00 PM').replace('AM',':00 AM');
}
//If AM or PM not included, add AM as default
if(!t.contains('AM') && !t.contains('PM'))
{
t += ' AM';
}
//Turn the time into a DateTime
string dtime = '01/01/15 '+t;
//Parse it and extract the time
string result = string.valueOf(DateTime.Parse(dtime).time()).replace('.000Z','');
return result;
}
Good luck!
Luis