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:
C# DateTime.Ticks equivalent in Java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With