title: Serializing java.util.Date
body: <p>Does anyone know how a java.util.Date gets serialized?  I mean explain to me exactly what each byte is?  I tried writing out a long then a date and I can see matches but there are other characters that I just don't get.</p>
  
  <p>Our application makes server requests with data which means it gets serialized from client to server.  The team that does stress testing uses a tool that captures these requests and modifies them, the problem is they want to process dates and I don't know how to interpret the byte stream.  The dude I am talking to seems willing to learn but so far I haven't found anything that I understand to point him to...</p>
  
  <p>Code I used:</p>
  
  <pre><code>  FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try
    {
     fos = new FileOutputStream("t.tmp");
     oos = new ObjectOutputStream(fos);
  
     Date today = new Date();
  
     oos.writeLong(today.getTime());
     oos.writeObject("Today");
     oos.writeObject(today);
  
     oos.close();
    }
    catch(FileNotFoundException e)
    {
     e.printStackTrace();
    }
    catch(IOException e)
    {
     e.printStackTrace();
    }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>The output from the above is:</p>
  
  <pre><code>"¬í w  ,áqÇ-t Todaysr java.util.DatehjKYt  xpw  ,áqÇ-x"
  </code></pre>
  
  <p>The long is "w  ,áqÇ-" so what is the stuff between the long and the Date object, i.e. "hjKYt  xp"</p>
  
  <p>NOTE some the blanks are unprintable characters NULL, SOH, backspace etc.  I understand that it is the hex value that matters.</p>
  
  <p>EDIT:</p>
  
  <p>Still having problems.  For some reason the serialized HTTP request does not serialize the date exactly like the answer I accepted says.  Very close but still different and I don't know why.  What's even odder is that when I simply serialize a date it seems to work fine.  FYI at worj we use Websphere 6.1  Here are some examples of what is being sent in the request:</p>
  
  <pre><code> lr_start_transaction("20000101");
  \\x0Ejava.util.Datehj\\x81\\x01KYt\\x19\\x03\\x00\\x00xpw\\x08\\x00\\x00\\x01,\\xE10\\x0BXxt\\x00\\x08
  
   lr_start_transaction("20000102");
  \\x0Ejava.util.Datehj\\x81\\x01KYt\\x19\\x03\\x00\\x00xpw\\x08\\x00\\x00\\x01,\\xE10&gt;\\x9Dxt\\x00\\x08
  
   lr_start_transaction("20000103");
  \\x0Ejava.util.Datehj\\x81\\x01KYt\\x19\\x03\\x00\\x00xpw\\x08\\x00\\x00\\x01,\\xE10z\\xDBxt\\x00\\x08
  </code></pre>
  
  <p>I have been able to identify most fields but not the actual time!  E.g the serialVersionUID is <code>hj\\x81\\x01KYt\\x19</code></p>
  
  <p>EDIT (FINAL):</p>
  
  <p>I found the date but it was no where near where I expected it!  It was well after the sample I had because other data fields were appearing I thought the date was done - it was just fluke that I noticed the hex pattern of the date I was looking for!  Example:</p>
  
  <pre><code> lr_start_transaction("20000101");
  \\x0Ejava.util.Datehj\\x81\\x01KYt\\x19\\x03\\x00\\x00xpw\\x08\\x00\\x00\\x01,\\xE10\\x0BXxt\\x00\\x08OTTST153t\\x00\\x06/Web2/t\\x00\\x044971t\\x00\\x0B12ce12f737d\\x00\\x00\\x01,\\xE10\\x0BXsq\\x00~\\x00\\x0Fw\\x08\\x00\\x00\\x00\\xDCk\\xE2T\\x80xt
  </code></pre>
  
  <p>The date value is right at the very end!</p>