java 序列化 date 存储,序列化java.util.Date

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.

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...

Code I used:

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();

}

EDIT:

The output from the above is:

"¬í w ,áqÇ-t Todaysr java.util.DatehjKYt xpw ,áqÇ-x"

The long is "w ,áqÇ-" so what is the stuff between the long and the Date object, i.e. "hjKYt xp"

NOTE some the blanks are unprintable characters NULL, SOH, backspace etc. I understand that it is the hex value that matters.

EDIT:

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:

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>\\x9Dxt\\x00\\x08

lr_start_transaction("20000103");

\\x0Ejava.util.Datehj\\x81\\x01KYt\\x19\\x03\\x00\\x00xpw\\x08\\x00\\x00\\x01,\\xE10z\\xDBxt\\x00\\x08

I have been able to identify most fields but not the actual time! E.g the serialVersionUID is hj\\x81\\x01KYt\\x19

EDIT (FINAL):

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:

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

The date value is right at the very end!

解决方案

/**

* 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Cannot deserialize value of type `java.util.Date`"错误通常有以下几个原因: 1. 日期格式不匹配:JSON数据中的日期格式与Java中的`java.util.Date`类型不匹配,导致无法解析。 2. 缺少日期格式转换配置:在将JSON数据转换为Java对象时,可能缺少配置将日期字符串转换为`java.util.Date`类型的格式。 为了更好地理解和解决该错误,可以参考以下示例代码来处理: ```java import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.DeserializationFeature; import java.util.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) { ObjectMapper objectMapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(Date.class, new CustomDateDeserializer()); objectMapper.registerModule(module); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); String jsonString = "{\"date\":\"2022-01-01\"}"; try { MyObject myObject = objectMapper.readValue(jsonString, MyObject.class); System.out.println(myObject.getDate()); } catch (Exception e) { e.printStackTrace(); } } static class CustomDateDeserializer extends JsonDeserializer<Date> { @Override public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String date = jsonParser.getText(); try { return format.parse(date); } catch (ParseException e) { throw new RuntimeException(e); } } } } class MyObject { private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } } ``` 在这个示例代码中,我们使用`ObjectMapper`来对JSON字符串进行反序列化。我们创建了一个自定义的`CustomDateDeserializer`类,用于将日期字符串转换为`java.util.Date`类型。然后,我们使用`objectMapper.readValue()`方法将JSON字符串转换为`MyObject`对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值