What is the equivalent of DateTime.FromOADate() in Java (double to Datetime in Java)

C# has a DateTime.FromOADate() method.

What is the equivalent of DateTime.FromOADate() in Java ?

This is my C# code :

var b = new byte[8];
b[0] = 0x20;
b[1] = 0x64;
b[2] = 0xa8;
b[3] = 0xac;
b[4] = 0xb6;
b[5] = 0x65;
b[6] = 0xe4;
b[7] = 0x40;
var dbl = BitConverter.ToDouble(b, 0);
var dt = DateTime.FromOADate(dbl);

This is the output :

2014-05-14T17:00:21

How can i convert this byte array to java?

Jon Skeet
people
quotationmark

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!

people

See more on this question at Stackoverflow