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?
最满意答案
您是否意识到您的二进制数据是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 adouble
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中的datetime等价物?(A datetime equivalent in java.sql ? (is there a java.sql.datetime ?))[2022-02-04]
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 ...
-
Java DateTime转换(Java DateTime convert)[2022-01-25]
你有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 ...