Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read WCF REST date from android

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.

like image 342
Carles Company Avatar asked Jan 12 '12 08:01

Carles Company


2 Answers

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

like image 85
P. Sohm Avatar answered Nov 12 '22 02:11

P. Sohm


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

like image 4
Eilistraee Avatar answered Nov 12 '22 02:11

Eilistraee