根据 MSDN,System.DateTime.Ticks
“表示自 0001 年 1 月 1 日午夜 12:00:00(UTC 时间 0:00:00)以来经过的 100 纳秒间隔的数量公历 0001 年 1 月 1 日)”。
DateTime
中有一个内部字段,UnixEpoch
,其值 621355968000000000L
应对应于 Unix 纪元(午夜,1 月 1 日, 1970 协调世界时)。 (我们可以从 new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
获得相同的值。)
我正在尝试根据 C# 刻度值在 Java 中创建一个 Date
:
这是一个重现问题的简单 Java 示例:
//C# System.DateTime.UnixEpoch = 621355968000000000;
//Java code:
//before Unix Epoch, in milliseconds
Date date = new Date(-621355968000000000L / 10000);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
df.format(date); // 0001-01-03 00:00:00.000 +0000
公历是否存在一种仅由一个平台考虑的差距?
最佳答案
Java Date 似乎在使用 Julian Calendar 时使用 Julian Calendar 作为日期,而 C# 在这种情况下使用 Gregorian Calendar 之前的日期。默认情况下,Joda-Time 还及时使用公历。
这似乎可行,但可能还有更好的方法。
DateTime dt = new DateTime(-621355968000000000L / 10000);
GregorianCalendar gc = new GregorianCalendar();
gc.set(GregorianCalendar.YEAR, dt.getYear());
gc.set(GregorianCalendar.MONTH, dt.getMonthOfYear() - 1);
gc.set(GregorianCalendar.DAY_OF_MONTH, dt.getDayOfMonth());
gc.set(Calendar.HOUR_OF_DAY, 0);
gc.set(Calendar.MINUTE, 0);
gc.set(Calendar.SECOND, 0);
gc.set(Calendar.MILLISECOND, 0);
Date date = gc.getTime();
关于java - 有 48 小时的差异,将 C# Ticks 转换为 Java 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31031356/