将.NET时间转换为Java时间是两天假

时间:2014-12-07 15:09:00

标签: java c# date datetime unix-timestamp

是的,我知道这个问题已经多次提出,但我仍然无法在.NET和Java之间得到一致的结果。

有许多posts和Google搜索results引用了常量:

long UNIX_EPOCH_IN_100NS_INTERVALS = 621355968000000000l;
long DOTNET_EPOCH_IN_MSEC_INTERVALS = -62135596800000l;

第一个声称是在UTC 0001-01-01 00:00:00(z yyyy-MM-dd HH:mm:ss)和UTC 1970-01-02 00之间的100ns间隔或“抽搐”的数量:00:00,第二个是.NET纪元和Java纪元之间的差异,以毫秒为单位。

帖子here在.NET世界中报告了类似的问题。

在下面的代码中,我计算了纪元之间的偏移量,然后与公布的数字进行比较:

SimpleDateFormat sdf = new SimpleDateFormat("z yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date;
try {
  date = sdf.parse("UTC 1970-01-01 00:00:00");
} catch (ParseException e) {
  e.printStackTrace();
  throw new RuntimeException("Stupid java dates !");
}
System.out.println("Unix Epoch Date: " + sdf.format(date));
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.setTime(date);
System.out.println("Unix Epoch Milliseconds: " + cal.getTimeInMillis());
try {
  date = sdf.parse("UTC 0001-01-01 00:00:00");
} catch (ParseException e) {
  e.printStackTrace();
  throw new RuntimeException("Stupid java dates !");
}
System.out.println("Calculated DotNet Epoch Date: " + sdf.format(date));
cal.setTime(date);
System.out.println("Calculated DotNet Epoch Milliseconds: " + cal.getTimeInMillis());
date = new Date(-62135596800000l);
System.out.println("Published DotNet Epoch Date: " + sdf.format(date));
cal.setTime(date);
System.out.println("Published DotNet Epoch Milliseconds: " + cal.getTimeInMillis());

生成输出:

Unix Epoch Date: UTC 1970-01-01 00:00:00
Unix Epoch Milliseconds: 0
DotNet Epoch Date: UTC 0001-01-01 00:00:00
DotNet Epoch Milliseconds: -62135769600000
Published DotNet Epoch Date: UTC 0001-01-03 00:00:00
Published DotNet Epoch Milliseconds: -62135596800000

任何帮助表示感谢。

0 个答案:

没有答案