I need to update the answer to this question, I needed to do the same, and I used the accepted answer, but the received date was incorrect, it gave me the date of the week earlier (I donβt know why). I know little about the Date type of a Java type, but I know that some of their methods are deprecated.
They say that I think this is the last right answer for this situation. Hope helps someone else
import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; ..... final Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT")); cal.setTime(value); // Where Value is a Date final long date = cal.getTime().getTime(); final String senddate = "/Date("+date+")/";
Note the use of dates using Upper case "D" , this is also necessary, as it causes problems with WCF (at least in my case, a lot of stuff is stuck for lowercase D)
I use it to use RESTful WCF with the Jackson library for JSON.
To add a complete answer, you can use this method for the date you want to serialize using jackson.
import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.annotate.JsonCreator; import org.codehaus.jackson.map.SerializerProvider; import org.codehaus.jackson.map.ser.std.SerializerBase; public class SerializeRESTDate extends SerializerBase<Date> { public SerializeRESTDate() {
And use it like in class class definition
@JsonSerialize(using = SerializeRESTDate.class) @JsonProperty("InspectionDate") public Date get_InspectionDate() { return _InspectionDate; }
source share