问题描述
如果我有一个 File
对象,如何在此GMT中获取此文件的 lastModified()
日期格式: 2011年6月23日星期一17:40:23 GMT
。
If i have a File
object how can i get the lastModified()
date of this file in this GMT format: Mon, 23 Jun 2011 17:40:23 GMT
.
例如,当我调用Java方法 lastModified()
并使用DateFormat对象来 getDateTimeInstance(DateFormat.Long,DateFormat.Long)
和还将 TimeZone
设置为GMT,文件日期将以不同的格式显示:
For example, when i call the java method lastModified()
on a file and use a DateFormat object to getDateTimeInstance(DateFormat.Long, DateFormat.Long)
and also set the TimeZone
to GMT, the file date displays in different format:
File fileE = new File("/Some/Path");
Date fileDate = new Date (fileE.lastModified());
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.Long, DateFormat.Long);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("file date " + dateFormat.format(fileDate));
此格式打印:
January 26, 2012 7:11:46 PM GMT
I感觉我将要以上面的格式获取它,而我只是想念这一天。我是否必须使用 SimpleDateFormat
对象代替?
I feel like i am close to getting it in the format above and i am only missing the day. Do i have to use instead the SimpleDateFormat
object?
推荐答案
SimpleDateFormat
,其模式如下:
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z");
更新:
Date d1 = new Date(file1.lastModified());
Date d2 = new Date(file2.lastModified());
您可以将它们进行如下比较:
You can compare them as follows:
d1.compareTo(d2);
d1.before(d2);
d1.after(d2);
为什么要在几秒钟的粒度下比较它们?
Why do you want to compare them at seconds granularity?
如果要获得以秒为单位的差异:
If you want to get the difference in seconds:
int diffInSeconds = (int) (d1.getTime() - d2.getTime()) / 1000;
这篇关于使用DateFormat对象获取正确的GMT格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!