Serializing java.util.Date

前端 未结 2 1077
春和景丽
春和景丽 2020-12-19 03:07

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 the

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 03:32

    /**
     * Save the state of this object to a stream (i.e., serialize it).
     *
     * @serialData The value returned by getTime()
     *         is emitted (long).  This represents the offset from
     *             January 1, 1970, 00:00:00 GMT in milliseconds.
     */
    private void writeObject(ObjectOutputStream s)
         throws IOException
    {
        s.writeLong(getTimeImpl());
    }
    

    therefore, it's the long value representing the offset from Jan 1 1970 00:00:00 GMT in milliseconds.

    EDIT: however this is preceeded and succeeded by some headers:

    0x73 - being the code for an ordinary object (TC_OBJECT)    
    0x72 - being the code for a class description (TC_CLASSDESC)    
    "java.util.Date" - the name of the class    
    7523967970034938905L - the serialVersionUID    
    0|0x02|0x01 - flags including SC_SERIALIZABLE & SC_WRITE_METHOD    
    0 - number of fields    
    0x78 - TC_ENDBLOCKDATA    
    null - there is no superclass descriptor    
    the time (long milliseconds since epoch)    
    0x78 - TC_ENDBLOCKDATA
    

提交回复
热议问题