首页 \ 问答 \ Java中的DateTime.FromOADate()相当于什么(Java中的Datetime是Datetime的两倍)(What is the equivalent of DateTime.FromOADate() in Java (double to Datetime in Java))

Java中的DateTime.FromOADate()相当于什么(Java中的Datetime是Datetime的两倍)(What is the equivalent of DateTime.FromOADate() in Java (double to Datetime in Java))

C#有一个DateTime.FromOADate()方法。

Java中DateTime.FromOADate()等价于什么?

这是我的C#代码:

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);

这是输出:

2014-05-14T17:00:21

我怎样才能将这个字节数组转换为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?

更新时间:2023-02-03 16:02

最满意答案

您是否意识到您的二进制数据是OLE自动化日期值的二进制抑制?

所以不要long ,你应该从你的数组中获得一个double值。

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);

结果是:

2014-05-14T17:00:21

我认为这个有效的问题应该是: Java中的DateTime.FromOADate()等价于什么?

答案是:

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);
}

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);
}

相关问答

更多
  • java.sql包有三个日期/时间类型: java.sql.Date - 仅限日期(无时间部分) java.sql.Time - 仅限时间(无日期部分) java.sql.Timestamp - 日期和时间 你想要最后一个: java.sql.Timestamp 。 如果您使用这些类型,则不需要调用特定的设置器; 只需使用: java.util.Date date = new Date(); Object param = new java.sql.Timestamp(date.getTime()); // ...
  • 只需构造一个没有任何参数的新的Date对象; 这会将当前日期和时间分配给新对象。 import java.util.Date; Date d = new Date(); 用零参数构造函数 的Javadocs的话来说: 分配一个Date对象并对其进行初始化,以便它表示它被分配的时间 ,测量到最接近的毫秒。 确保你使用java.util.Date而不是java.sql.Date - 后者没有一个零arg构造函数,并且有一些不同的语义是一个完全不同的会话的话题。 :) Just construct a new ...
  • 你有24小时的时间格式。 所以你必须使用大写字母H几个小时: H小时(0-23) h上午/下午(1-12) private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm"); 有关更多详细信息,请参阅SimpleDateFormat的文档。 You have 24 hours time format. So you have to use uppercase H for hours: H Hour in day (0-23 ...
  • 在Java中: long fromBytes = -8_586_803_256_090_942_249L; // Reverse sign bit fromBytes = fromBytes ^ 0x8000_0000_0000_0000L; Instant cSharpEpoch = Instant.parse("0001-01-01T00:00:00Z"); // 100 nanosecond units or 10^-7 seconds final in ...
  • 查看GregorianCalendar中的方法: http : //download.oracle.com/javase/6/docs/api/java/util/GregorianCalendar.html (请阅读说明中的详细用法示例) Lookup the methods in GregorianCalendar: http://download.oracle.com/javase/6/docs/api/java/util/GregorianCalendar.html (Do read the det ...
  • 如果这是UNIX时间戳,那么您可以使用此函数进行转换(借用http://codeclimber.net.nz/archive/2007/07/10/convert-a-unix-timestamp-to-a-.net- datetime.aspx ) static DateTime ConvertFromUnixTimestamp(double timestamp) { DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); retur ...
  • 您是否意识到您的二进制数据是OLE自动化日期值的二进制抑制? 所以不要long ,你应该从你的数组中获得一个double值。 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( ...
  • 我假设你回来的日期字符串基本上是dt.toString() 您需要使用您创建的格式化程序格式化日期,否则格式不相关。 尝试这个: System.out.println(formatter.print(dt)); 当您使用格式化程序设置日期时,它纯粹使用您定义的格式来解析字符串...这对DateTime对象没有任何影响,因此在将其转换回字符串时仍需要使用格式化程序 I assume the date string you're getting back is basically dt.toString() ...
  • 您可以使用SimpleDateFormat设置文本字段的文本和当前时间 Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(date); formatText = new JFormattedTextField(createFormatter("####-##- ...
  • 没有任何直接的方法可以使用Java Time API查询该信息,但您可以计算它。 public static void main(String[] args) { long ordinal = LocalDate.now().toEpochDay() + (146097 * 5L) - (31L * 365L + 7L); System.out.println(ordinal); } 今天运行,2016年4月19日,它输出736073 ,这与Python的输出一致。 从LocalDate ...

相关文章

更多

最新问答

更多
  • 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
  • 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
  • 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
  • ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
  • Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
  • 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
  • 功能包装避免重复(wrap of functions avoid duplicating)
  • Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
  • 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
  • VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
  • 大学电脑四级对初学者来说要多久能过
  • 特殊字符删除?(Special characters remove?)
  • Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
  • 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
  • Javaweb开发,技术路线是什么?该怎么写?
  • JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
  • 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
  • Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
  • 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
  • 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
  • “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
  • maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
  • 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
  • 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
  • java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
  • 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
  • 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
  • 嵌套的模板可能性(Nested template possibilities)
  • 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
  • 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)