title: Java : SimpleDateFormat - Unable to parse a string to a date of desired format
body: <p>I am trying to parse one string to a format <code>yyyy-MM-dd</code> and one to <code>yyyy-MM-dd HH:mm:ss</code>.
  I am using <code>SimpleDateFormat</code>.</p>
  
  <p>I have used the same method successfully earlier, but now something is going wrong. I get the date in the standard format i.e. <code>Tue Mar 05 00:00:00 GMT+05:30 2012</code> (for <code>yyyy-MM-dd</code>) and <code>Mon Mar 05 13:01:35 GMT+05:30 2012</code> (for <code>yyyy-MM-dd HH:mm:ss</code>) where as I need them to be <code>2012-03-05</code> and <code>2012-03-05 13:01:35</code> repectively.</p>
  
  <p>This is what I do : </p>
  
  <pre><code>Date today = new Date();
  SimpleDateFormat tsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  try {
      today = tsdf.parse(tsdf.format(new Date()));
  } catch(Exception e) {
      System.out.println("Error occurred"+ e.getMessage());
  }
  
  System.out.println(today);
  </code></pre>
  
  <p><strong>AND</strong></p>
  
  <pre><code>SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  try {
      // I recieve a string in this format here (refers to 5 march 2012).
      myDate = sdf.parse('2012-03-05');            
      System.out.println(myDate);
  } catch(Exception e) {
      System.out.println("Error occurred "+ e.getMessage());
  }
  </code></pre>