Reputation: 6188
Microsoft uses a datetime format that looks like this: \/Date(1399017480000-0000)\/
I would like to write a Java method that converts a Java date to Microsoft's datetime format. Can anyone point to a resource that explains how to do this?
Upvotes: 0
Views: 1707
Reputation: 1503409
Assuming you're just talking about java.util.Date
, it's as simple as:
public static String toJson(Date date) {
return "\\/Date(" + date.getTime() + "-0000)\\/";
}
(That's assuming the backslashes should really be part of the string... it's not clear whether you actually want them or not, but you can remove them easily enough.)
Basically the first part of the string is just the number of milliseconds since the Unix epoch, which is the value in a java.util.Date
as well, as accessed via getTime()
.
The "0000" bit is a UTC offset, but as a java.util.Date
has no concept of a time zone or an offset, you can just use 0000 and you'll end up representing the right instant in time. If you were starting with a java.util.Calendar
value you could convert the offset as well, if you really wanted to.
Upvotes: 3