在文件夹显示中,文件在过去15分钟内被修改/更新 [英] In a folder display the files was modified/updated on last 15 minutes

查看:64
本文介绍了在文件夹显示中,文件在过去15分钟内被修改/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我尝试让文件夹中的文件最近15分钟更新。

但我更新了文件最近在代码中。

任何人请帮助我。



我尝试过的事情:



Hello,

I try to get the files in the folder was updated last 15 minutes.
But am getting the files updated recently in code.
Any one please help me on this.

What I have tried:

File[] files = dir.listFiles();
File lastModifiedFile = files1[0];
for (int i = 1; i < files1.length; i++)
{
   if (lastModifiedFile.lastModified() < files[i].lastModified()) {
      lastModifiedFile = files[i];
      System.out.println(lastModifiedFile.getName());
   }
}

推荐答案

试试这个:

Try this:
long last15 = Instant.now().toEpochMilli(); // number of milliseconds since the epoch
last15 -= 15 * 60 * 1000; // less 15 minutes
File dir = new File(dirPath);
File[] files = dir.listFiles();
if (files == null || files.length == 0) 
{
    return null;
}
for (int i = 0; i < files.length; i++)
{
    if (files[i].lastModified() > last15)
    { 
        System.out.println(files[i]);
    }
}


lastModified 时间以毫秒为单位表示时代。所以你需要的只是当前时间和相同格式的差异。



15分钟它可能是

The lastModified time is represented in milliseconds since the epoch. So all you need is the current time and the difference in the same format.

For 15 minutes it could be
import java.time.Instant;
// ...
Instant timestamp = Instant.now();
long timeLimitMillis = instant.toEpochMilli() - (15 * 60 * 1000);
// ...
if (files[i].lastModified() >= timeLimitMillis) 
{
    // Has been modified during the last 15 minutes
}


这篇关于在文件夹显示中,文件在过去15分钟内被修改/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆