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?
Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes. For example: Class. Description. LocalDate. Represents a date (year, month, day (yyyy-MM-dd)) LocalTime. Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns))
In Java, there is more than one way of getting the difference between two dates, which are as follows: Let's understand all three ways, one by one and understand how all these three classes are used to calculate the date difference in Java. The SimpleDateFormat class is used for formatting and parsing the data.
The DateTime.FromOADate() method in C# is used to return a DateTime equivalent to the specified OLE Automation Date. Syntax. Following is the syntax − public static DateTime FromOADate (double val); Above, Val is the OLE Automation Date value. Example. Let us now see an example to implement the DateTime.FromOADate() method −
Java Dates. Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes. For example: If you don't know what a package is, read our Java Packages Tutorial.
Did you realize that your binary data is the binary represantation of an OLE Automation date value?
So instead of getting long
, you should get a double
value from your array.
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);
Console.WriteLine("{0:s}", dt);
Result is :
2014-05-14T17:00:21
I think the valid question should be: What is the equivalent of DateTime.FromOADate() in Java ?
Answer is:
public static Date fromDoubleToDateTime(double OADate)
{
long num = (long) ((OADate * 86400000.0) + ((OADate >= 0.0) ? 0.5 : -0.5));
if (num < 0L) {
num -= (num % 0x5265c00L) * 2L;
}
num += 0x3680b5e1fc00L;
num -= 62135596800000L;
return new Date(num);
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With