有48小时的差异,将C#记号转换为Java时间戳
根据MSDN,有48小时的差异,将C#记号转换为Java时间戳,java,c#,datetime,calendar,gregorian-calendar,Java,C#,Datetime,Calendar,Gregorian Calendar,根据MSDN,System.DateTime.Ticks“表示自0001年1月1日午夜12:00:00(公历0001年1月1日0:00:00 UTC)以来经过的100纳秒间隔数” DateTime,UnixEpoch中有一个内部字段,其值为62135596800000000l,应与Unix纪元(1970年1月1日午夜UTC)相对应。(我们可以从新日期时间(1970,1,1,0,0,0,0,System.DateTimeKind.Utc)中获得相同的值;) 我正试图基于C#ticks值在Java中
System.DateTime.Ticks
“表示自0001年1月1日午夜12:00:00(公历0001年1月1日0:00:00 UTC)以来经过的100纳秒间隔数”
DateTime
,UnixEpoch
中有一个内部字段,其值为62135596800000000l
,应与Unix纪元(1970年1月1日午夜UTC)相对应。(我们可以从新日期时间(1970,1,1,0,0,0,0,System.DateTimeKind.Utc)中获得相同的值;
)
我正试图基于C#ticks值在Java中创建一个日期
:
下面是一个简单的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在使用公历时使用了儒略历,而在本例中,C#在使用公历之前使用了公历。默认情况下,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();
什么是
System.DateTime.UnixEpoch
?我在MSDN中看不到这一点。@HovercraftFullOfEels:我认为上面所有的代码都是Java;OP从C#中获取一个值,并试图从中创建JavaDate
。C#和Java平台之间存在数据交换。因此,需要了解哪个平台日历具有“特定”行为。@T.J.Crowder System.DateTime.UnixEpoch是内部常量。我们可以通过新的DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc)计算相同的值@vlasov-您是否尝试过使用类似于Jodatime或java 8 time API的东西在0001年1月1日Utc 0:00:00创建一个DateTime,并将其值与-62135596800000000L/10000
进行比较?