This looks like it works... basically ToBinary
just returns a representation where the bottom 58 bits are the ticks since the BCL epoch in UTC. This code just reverses that
private static final long UNIX_EPOCH = 62135596800000L;
public static Date fromDateTimeBinary(long value) {
// Mask off the top bits, which hold the "kind" and
// possibly offset.
// This is irrelevant in Java, as a Date has no
// notion of time zone
value = value & 0x3fffffffffffffffL;
// A tick in .NET is 100 nanoseconds. So a millisecond
// is 10,000 ticks.
value = value / 10000;
return new Date(value - UNIX_EPOCH);
}
I've tested that for a "local" DateTime
and a "UTC" DateTime
. It will treat an "unspecified" DateTime
as being in UTC.
Overall it's not ideal, and you should talk to wherever you're getting the data from to try to change to a more portable format, but until then this should help. Do test it further though!