File.lastmodified()生成错误的日期和月份 [英] File.lastmodified() generates wrong date and month
问题描述
我使用File.getlastmodified()来获取文件的最后修改,但是由于某种原因(可能是UIT),其打印错误的日期和月份(年份还可以)
我最后通过上述方法进行了修改并将其保存为Long
//理想情况下,这应该将最后修改的内容转换为人类可交易的格式但返回的月份和日期不正确
like ----> 2018年11月27日
显示为---> 2018年10月28日
c = Calendar.getInstance();长时期= files [i] .lastModified();epoch + = 32400L;c.setTimeInMillis(epoch);//im将毫秒转换为人类可读的格式int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH);int day = c.get(Calendar.DATE);
该值不正确.您错误地使用了 Calendar
.查看文档,您可以看到 Month
的返回值.
get和set的字段号指示月份.这是日历特定的值.阳历和朱利安历法中的第一个月是1月,0.最后一个取决于一年中的月份数.
源 >
现在,对于一天来说,我怀疑增加32秒可能是问题所在.但很可能是时区.实际上,该方法在GMT上返回了一个值.
请注意,我已检查并修改了"2019-04-17 14:52:13"
的文件,并获得正确的结果.另外,您可以使用 SimpleDateFormat
实例设置 Calendar
的格式,而不用像这样提取值.
私有静态字符串格式Epoch(长时期){返回新的SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(epoch));}
或者,我们永远都不会提及这个问题,而是使用带有 Instant
的最新API来显示日期.
私有静态字符串格式Epoch(长时期){DateTimeFormatter格式化程序= DateTimeFormatter.ISO_LOCAL_DATE_TIME;返回formatter.format(Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()));}
瞬时是格林尼治标准时间的日期时间,因此我们在使用 DateTimeFormatter
对其进行格式设置之前添加语言环境时区,以提供类似以下内容的值:
<代码> 2019-04-17T14:52:13.118
Im using File.getlastmodified() to get last modifies of files but for some reason (probably UIT) its printing wrong date and month (year is fine)
Im getting last modifed with the above method and saving it to Long
//This should ideally conver the last modified to human radable format but its returning wrong month adn date
like----> 27/11/2018
shows as---> 28/10/2018
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);
The value is not incorrect.
You are using the Calendar
incorrectly. Checking the documentation you can see the value returned for Month
.
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
Now, for the day, I would suspect the addition of 32 seconds could be the problem. But most probably the timezone. Indeed, the method return a value on GMT.
Note that I have check with a file modified the "2019-04-17 14:52:13"
and get the correct result. Also, you can format the Calendar
using a SimpleDateFormat
instance instead of extracting the value like this.
private static String formatEpoch(long epoch) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(epoch));
}
Or, we can never mention this enough, using a more recent API for date with Instant
.
private static String formatEpoch(long epoch) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
return formatter.format(Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()));
}
An instant is a date-time at GMT, so we add the locale timezone before formatting it with a DateTimeFormatter
to provide a value like :
2019-04-17T14:52:13.118
这篇关于File.lastmodified()生成错误的日期和月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!