title: Android Date Timezones driving me nuts
body: <p>I have a Android app that works with GPS module and a SQLite database. Everything sounds so nice in the beginning, but the horror comes when I need to work with Date() and timezones.</p>
  
  <p><strong>A little background</strong></p>
  
  <p>The GPS module has the possibility to get date from the satellite. This is a great thing and I need it in my application to assure valid data. The date returned from it is GMT time.
  This Date is inserted in a field in local database as StartTime. The StartTime is stored encrypted with AES. When I decrypt the StartTime, I need to convert it as a Date and keep the format as GMT.</p>
  
  <p><strong>What I do</strong> 
  Get the data from the GPS in LocationListener:</p>
  
  <pre><code>gpsDate = new Date(location.getTime());
  </code></pre>
  
  <p>by having new Date() by default the GPS date is converted in the current time zone of the Device. So instead of having a nice <strong>Sun Dec 05 14:00:00 GMT 2010</strong> time, I get <strong>Sun Dec 05 16:00:00 GMT+2 2010</strong>. Why does it do that ? I just wanted a new Date out of the getTime().</p>
  
  <p>I insert the data in database:</p>
  
  <pre><code>SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  outputFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 
  String date = outputFormat.format(gpsDate);
  </code></pre>
  
  <p>I retrieve the Date when I need it:</p>
  
  <pre><code>SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
  String myStored = {decrypy from the database}
  Date myStartDate = outputFormat.parse(myStoredDate);
  </code></pre>
  
  <p>myStored shows: <strong>2010-12-05 14:00</strong></p>
  
  <p>myStartDate shows: <strong>Sun Dec 05 16:00:00 GMT+2 2010</strong></p>
  
  <p>There is something I am missing in here. I need to be able to get the Date in GMT format, insert it, convert it, compare it, calculate differences. I need the Date in users Timezone only when I need to show it to him.</p>
  
  <p>Please illuminate me.</p>