title: JSON date to Java date?
body: <p>I could not find this anywhere. I fetch some JSON from an API that returns standard JSON dates. You can see the format by running this code in a JavaScript console:</p>
  
  <pre><code>&gt; new Date().toJSON();
  "2010-10-27T11:58:22.973Z"
  </code></pre>
  
  <p>Well, actually, the API I'm working with is not returning the millisecond part, and sometimes it returns a timezone instead of <code>Z</code>, so dates can look like any one of these:</p>
  
  <ul>
  <li><code>2010-10-27T11:58:22Z</code></li>
  <li><code>2010-10-27T11:58:22+03:00</code></li>
  </ul>
  
  <p>Parsing these kinds of dates is somewhat cumbersome. Is there any way to parse these kinds of dates, using <a href="http://www.json.org/java/" rel="noreferrer"><code>org.json</code></a>?</p>
  
  <p>My current solution is:</p>
  
  <pre><code>public static Date parseDateTime(String dateString) {
      if (dateString == null) return null;
      DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
      if (dateString.contains("T")) dateString = dateString.replace('T', ' ');
      if (dateString.contains("Z")) dateString = dateString.replace("Z", "+0000");
      else
          dateString = dateString.substring(0, dateString.lastIndexOf(':')) + dateString.substring(dateString.lastIndexOf(':')+1);
      try {
          return fmt.parse(dateString);
      }
      catch (ParseException e) {
          Log.e(Const.TAG, "Could not parse datetime: " + dateString);
          return null;
      }
  }
  </code></pre>
  
  <p>Ugh!</p>