最近项目中遇见这样一个关于时间戳的问题,我们的后台是.net , 具体的需求是这样的。我向后台请求,下载一个二进制的文件,然后就开始了漫长的解析二进制文件的过程,在朋友的帮助下,文件解析其他数据均解析成功,只有一个拿到的时间戳,转化出来的时间,总是不正确,然后经过一番查找,最终找到解决办法。分享给有同样需求的小伙伴儿,帮人就是帮自己嘛
private static final long TICKS_AT_EPOCH = 621355968000000000L; private static final long TICKS_PER_MILLISECOND = 10000; private static TimeZone timeZone = TimeZone.getDefault();
/** * 将.net的时间戳转化为Java的时间戳 */ public static String fromDnetToJdate(long longTime) { Calendar calendar = Calendar.getInstance(timeZone); calendar.setTimeInMillis((longTime - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND); calendar.setTimeInMillis(calendar.getTimeInMillis() - calendar.getTimeZone().getRawOffset()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str= simpleDateFormat.format(calendar.getTime()); return str; }
亲测有效。
参考链接如下:https://blog.csdn.net/ksf54411376/article/details/77932033