title: How to convert string into datetime format in salesforce apex code?
body: <p>I want to change datetime format so that first of all I got datetime and after that I have convert it to string with different format. Now I want to again convert this into datetime format but this gives an error, invalid date time.  My code is:</p>
  
  <pre><code>DateTime startDT2 = DateTime.newInstance(selectedDate.addDays(1), initialEndTime);
  system.debug('select dt2&gt;&gt;&gt;'+ startDT2); **output**=2014-12-09 8:00:00
  
  String myDate = startDT2.format('M/d/yyyy h:mm a'); 
  system.debug('select mydate&gt;&gt;&gt;'+ myDate); **output**=12/9/2014 1:00 AM
  Datetime dt = Datetime.valueOf(myDate);
  system.debug('date_string &gt;&gt;&gt;'+ dt);    **output**=invalid date/time 
  </code></pre>
  
  <p>How to solve my problem?</p>