[英]How to convert the file last modified timestamp to a date?
How do I convert File#lastModified()
to a real date?如何将
File#lastModified()
转换为真实日期? The format is not really important.格式并不重要。
Date d = new Date(file.lastModified());
lastModified()
returns the milliseconds since 1970-01-01, and the Date
class stores its time also in the same way. lastModified()
返回自 1970-01-01 以来的毫秒数, Date
类也以相同的方式存储其时间。 The Date(long)
constructor takes these milliseconds, and initializes the Date
with it. Date(long)
构造函数需要这些毫秒,并用它初始化Date
。
Just you use the SimpleDateFormat
class to convert long to date.只需使用
SimpleDateFormat
类将 long 转换为 date。 Only you execute code:只有你执行代码:
new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(
new Date(new File(filename).lastModified())
);
What you get is a long number representing the number of millis elapsed from Jan 1st, 1970. That's the standard way of representing dates.你得到的是一个很长的数字,表示从 1970 年 1 月 1 日起经过的毫秒数。这是表示日期的标准方式。
try this:尝试这个:
java.util.Date myDate = new java.util.Date(theFile.lastModified());
and now you have a Date object at hand.现在您手头有一个 Date 对象。
You can use SimpleDateFormat to print that date in a cuter way.您可以使用SimpleDateFormat以更可爱的方式打印该日期。
Get the last modified timestamp, as described in the duplicate of your question获取上次修改的时间戳,如您问题的副本中所述
Create a new Date
object, or Calendar
object.创建一个新的
Date
对象或Calendar
对象。 new Date(timestamp)
. new Date(timestamp)
。 Or Calendar.getInstance()
and then call setTimeInMillis(timestamp)
.或者
Calendar.getInstance()
然后调用setTimeInMillis(timestamp)
。 As the name suggests, the timestamp is actually a number of milliseconds (since Jan 1st 1970)顾名思义,时间戳实际上是一个毫秒数(自 1970 年 1 月 1 日以来)
You can then format the date via java.text.SimpleDateFormat
然后您可以通过
java.text.SimpleDateFormat
格式化日期
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.