Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

android - JSON date converted from ASP - Need converting to Java

I have a slight problem with my JSON string which I have converted from ASP.NET.

The JSON string that C# has converted for me looks like this: /Date(1338199919727)/

I guess it converts it into milliseconds or something, but how do I get a Java-date out of this huge integer?

I need to get the date displayed properly on my Android app (as a string in a specific format (dd-MM-yyyy : HH), that's why I need it to be in Java. Do I have to manually make some form of converter myself, and figure out how to do it? Or is there some sweet, easy way of doing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

but how do i get a java-date out of this huge integer?

Ah, that part's easy:

Date dt = new Date(1338199919727L);

You are quite right, it's milliseconds-since-The-Epoch (Jan 1st at midnight, 1970). And Java's Date object has a constructor that accepts that very value.

In general, most JSON parsers have the concept of a "reviver" function or class that you can pass to them, which will let you pre-process values as the JSON is being deserialized. I don't know Android's JSON parser well enough to know if it has one. If not (which would be a bit backward), you'd have to walk the resulting object graph looking for properties that had become strings and converting them to dates after-the fact.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...