如何将文件上次修改时间戳转换为日期?

时间:2023-01-26 16:12:29

How do I convert File#lastModified() to a real date? The format is not really important.

如何将File#lastModified()转换为实际日期?格式并不重要。

4 个解决方案

#1


41  

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. The Date(long) constructor takes these milliseconds, and initializes the Date with it.

lastModified()返回自1970-01-01以来的毫秒数,而Date类也以相同的方式存储其时间。 Date(long)构造函数需要这些毫秒,并使用它初始化Date。

#2


13  

Just you use the SimpleDateFormat class to convert long to date. Only you execute code:

只是你使用SimpleDateFormat类来转换为long。只有你执行代码:

new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(
    new Date(new File(filename).lastModified()) 
);

#3


5  

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以可行的方式打印该日期。

#4


2  

  1. Get the last modified timestamp, as described in the duplicate of your question

    获取上次修改的时间戳,如问题副本中所述

  2. Create a new Date object, or Calendar object. new Date(timestamp). Or Calendar.getInstance() and then call setTimeInMillis(timestamp). As the name suggests, the timestamp is actually a number of milliseconds (since Jan 1st 1970)

    创建一个新的Date对象或Calendar对象。新日期(时间戳)。或者Calendar.getInstance()然后调用setTimeInMillis(timestamp)。顾名思义,时间戳实际上是几毫秒(自1970年1月1日起)

  3. You can then format the date via java.text.SimpleDateFormat

    然后,您可以通过java.text.SimpleDateFormat格式化日期

#1


41  

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. The Date(long) constructor takes these milliseconds, and initializes the Date with it.

lastModified()返回自1970-01-01以来的毫秒数,而Date类也以相同的方式存储其时间。 Date(long)构造函数需要这些毫秒,并使用它初始化Date。

#2


13  

Just you use the SimpleDateFormat class to convert long to date. Only you execute code:

只是你使用SimpleDateFormat类来转换为long。只有你执行代码:

new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(
    new Date(new File(filename).lastModified()) 
);

#3


5  

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以可行的方式打印该日期。

#4


2  

  1. Get the last modified timestamp, as described in the duplicate of your question

    获取上次修改的时间戳,如问题副本中所述

  2. Create a new Date object, or Calendar object. new Date(timestamp). Or Calendar.getInstance() and then call setTimeInMillis(timestamp). As the name suggests, the timestamp is actually a number of milliseconds (since Jan 1st 1970)

    创建一个新的Date对象或Calendar对象。新日期(时间戳)。或者Calendar.getInstance()然后调用setTimeInMillis(timestamp)。顾名思义,时间戳实际上是几毫秒(自1970年1月1日起)

  3. You can then format the date via java.text.SimpleDateFormat

    然后,您可以通过java.text.SimpleDateFormat格式化日期