7

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Carles Company
  • 7,118
  • 5
  • 49
  • 75

3 Answers3

8

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

Community
  • 1
  • 1
P. Sohm
  • 2,842
  • 2
  • 44
  • 77
4

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

Community
  • 1
  • 1
Eilistraee
  • 8,220
  • 1
  • 27
  • 30
0

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.

John J Smith
  • 11,435
  • 9
  • 53
  • 72