我使用File.getlastmodified()来获取文件的最后修改,但由于某种原因(可能是UIT),其打印错误的日期和月份(年份很好)
我最后用上述方法修改并将其保存到Long
//理想情况下,这应该将最后修改的内容转换为人类可交易的格式,但返回的月份和日期错误
喜欢----> 27/11/2018
显示为-> 2018年10月28日
c = Calendar.getInstance();
Long epoch = files[i].lastModified();
epoch+=32400L;
c.setTimeInMillis(epoch); //im converting the milliseconds to human readable formate
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DATE);
Run Code Online (Sandbox Code Playgroud)
该值不正确。您使用Calendar不正确。查看文档,您可以看到返回的值Month。
get和set的字段号指示月份。这是日历特定的值。阳历和朱利安历中的第一月是一月,即0。最后一个取决于一年中的月份数。
现在,对于一天来说,我怀疑可能会增加32秒。但很可能是时区。实际上,该方法在GMT上返回值。
请注意,我已经检查了修改过的文件,"2019-04-17 14:52:13"并获得了正确的结果。另外,您可以Calendar使用SimpleDateFormat实例来格式化格式,而不是像这样提取值。
private static String formatEpoch(long epoch) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(epoch));
}
Run Code Online (Sandbox Code Playgroud)
或者,我们永远也不能用足够多的时间来提及这个问题,使用一个更新的API作为date Instant。
private static String formatEpoch(long epoch) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
return formatter.format(Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()));
}
Run Code Online (Sandbox Code Playgroud)
即时时间是格林尼治标准时间的日期时间,因此我们在使用对其进行格式设置之前添加语言环境时区,DateTimeFormatter以提供类似以下的值:
2019-04-17T14:52:13.118
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |