Hi Shradha,
Here is the way to do it:
String TimeString = ‘4:33:34 PM’;
String Regex = ‘(\\d{1,2}):(\\d{1,2}):(\\d{1,2}) ([PA]M)’;
Pattern p = Pattern.compile( Regex );
Matcher m = p.matcher( TimeString );
if ( m.matches() ){
Integer Hours = Integer.valueOf( m.group(1) )
, Minutes = Integer.valueOf( m.group(2) )
, Seconds = Integer.valueOf( m.group(3) )
, PmShift = m.group(4) == ‘PM’ ? 12 : 0
;
Time t = Time.newInstance( Hours + PmShift , Minutes , Seconds , 0 );
System.debug( (”+t).substring(0,(”+t).indexOf(‘.’)) );
}