请教各位博友2个问题:
1.Json中表示日期的格式Date(1583570856087+0800) 这个是啥标准?
2.Java用啥工具可以将Entity中的Date类型序列化为Date(1583570856087+0800) 这种json的日期格式?
补充:
具体了解下来这个格式好像是微软的处理方式(https://docs.microsoft.com/en-us/dotnet/api/system.web.script.serialization.javascriptserializer?redirectedfrom=MSDN&view=netframework-4.8)
Date object, represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.
The maximum supported date value is MaxValue (12/31/9999 11:59:59 PM) and the minimum supported date value is MinValue (1/1/0001 12:00:00 AM).
WCF里面关于日期格式的序列化方式:(https://www.codenong.com/44934054/)
public static string MsJson(DateTime value)
{
long unixEpochTicks = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;
long ticks = (value.ToUniversalTime().Ticks - unixEpochTicks) / 10000;
if (value.Kind == DateTimeKind.Utc)
{
return String.Format("/Date({0})/", ticks);
}
else
{
TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime());
string sign = ts.Ticks < 0 ?"-" :"+";
int hours = Math.Abs(ts.Hours);
string hs = (hours < 10)
?"0" + hours
: hours.ToString(CultureInfo.InvariantCulture);
int minutes = Math.Abs(ts.Minutes);
string ms = (minutes < 10)
?"0" + minutes
: minutes.ToString(CultureInfo.InvariantCulture);
return string.Format("/Date({0}{1}{2}{3})/", ticks, sign, hs, ms);
}
}
The AJAX JSON serializer in ASP.NET encodes a DateTime instance as a JSON string. During its pre-release cycles, ASP.NET AJAX used the format "@ticks@", where ticks represents the number of milliseconds since January 1, 1970 in Universal Coordinated Time (UTC). A date and time in UTC like November 29, 1989, 4:55:30 AM would be written out as "@62831853071@." Although simple and straightforward, this format cannot differentiate between a serialized date and time value and a string that looks like a serialized date but is not meant to be deserialized as one. Consequently, the ASP.NET AJAX team made a change for the final release to address this problem by adopting the "/Date(ticks)/" format.
The new format relies on a small trick to reduce the chance for misinterpretation. In JSON, a forward-slash (/) character in a string can be escaped with a backslash () even though it is not strictly required. Taking advantage of this, the ASP.NET AJAX team modified JavaScriptSerializer to write a DateTime instance as the string "/Date(ticks)/" instead. The escaping of the two forward-slashes is superficial, but significant to JavaScriptSerializer. By JSON rules, "/Date(ticks)/" is technically equivalent to "/Date(ticks)/" but the JavaScriptSerializer will deserialize the former as a DateTime and the latter as a String. The chances for ambiguity are therefore considerably less when compared to the simpler "@ticks@" format from the pre-releases.