What's the easiest way to read a .NET DateTime sent over a WCF REST service on an android program? The dates are serialized in the following format: Data=/Date(1326273723507+0100)/
Is there an easy way to deserialize this dates?
Thanks.
What's the easiest way to read a .NET DateTime sent over a WCF REST service on an android program? The dates are serialized in the following format: Data=/Date(1326273723507+0100)/
Is there an easy way to deserialize this dates?
Thanks.
well I had exactly the same problem, I resolved it with that simple code
public class Json {
/**
* Convertit une date Json en java.util.Date
* @param jsonDate
* @return
*/
public static Date JsonDateToDate(String jsonDate)
{
// "/Date(1321867151710+0100)/"
int idx1 = jsonDate.indexOf("(");
int idx2 = jsonDate.indexOf(")") - 5;
String s = jsonDate.substring(idx1+1, idx2);
long l = Long.valueOf(s);
return new Date(l);
}
}
if you use gson, you can also use that solution : gson serialization of Date field in MS WCF compatible form
The XML Serializer serializes Datetime instances using the Ticks property for best precision (the Ticks is the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001). It's the number you are seeing here, followed by +0100 which means GMT + 1:00
To translate ticks into a valid java date, see Jon Skeet's (All roads lead to Jon Skeet) answer here:
I'm sending and receiving dates to/from a WCF REST service. After much mucking around converting from ticks to milliseconds since the Epoch (using Jon Skeet's answer), I decided just to store the dates as the Java long values (from date.getTime()) so that I don't do any conversion to and from the Android device. If I need to see them on the .NET side, then I'll do the conversion. So, if you have control over the WCF end and the database, keep them as longs.