Memo: [Java] Identify files with an update date of 1 day or less

background

A file that is updated every second is created every day at midnight with a new date. I wanted to identify the latest file other than the file being updated. I will write down the code at that time. こんなイメージ.png

** Get update date **

Get the modified date with File.lastModified (). However, since the returned value is a Long type and a list of numbers, use SimpleDateFormat so that it can be identified. ** " yyyy / MM / dd HH: mm: ss " ** is displayed in the format.

test.java


import java.io.File;
import java.text.SimpleDateFormat;

public class test{

  public static void main(String[] args){

    String filepath = "C:/tedkuma/BOX/20180729.csv";             //Referenced directory
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//Date time format definition
    File targetFile = new File(filepath);
    Long lastModified = targetFile.lastModified();   //LastModified to get the modified date()use.
    String update_time = simpleDateFormat.format(lastModified);  //Format the value that came in long with simpleDateFormat
    System.out.println(update_time);
  }
}

Let's run it. I was able to get the update date and time. 1.png

** Try to combine with the process of exporting the file name **

Use the previously written Check the contents of the directory to write out the names and modification dates of multiple files.

test.java


import java.io.File;
import java.text.SimpleDateFormat;

public class test
{
    public static void main(String[] args)
    {
        String filepath = "C:/tedkuma/BOX/";

        File dir = new File(filepath);
        File[] list = dir.listFiles();          //Get a list of files using listFiles
        for(int i=0; i<list.length; i++) {
          String filename = list[i].getName();
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//Date time format definition
          File targetFile = new File(filepath+filename);
          Long lastModified = targetFile.lastModified();
          String update_time = simpleDateFormat.format(lastModified);
          System.out.println(update_time+":"+filename);
        }
    }
}

Let's run it. The file name and update date could be displayed. 2.png

** Calculate time **

Use the Calendar class to calculate the time. (Added import java.util.Calendar;) You can get the current time with ** Calendar.getInstance () **. With the variable .add (Calendar.DATE, 1) ;, the time after 1 day, if you change the DATE to MINUTE, after 1 minute, You can also get the time one day ago or one minute ago by changing the second argument 1 to -1.

Since the lastModified (long type) that got the update date cannot be directly converted to the Calendar type I am using a Date type that can be converted to either. (Added import java.util.Date;)

You can use ** compareTo () to compare the size **. When you write ** A . CompareTo ( B **) ** A > ** B ** When A is in front, -1 ** A ** < B ** When A is later, 1 ** A ** = ** B ** At the same time, 0 is returned.

** Identify files that have been updated within 1 day from updated files **

I don't want to get the updated files that are updated every second I put in the if condition that the update time is within 1 day and 10 minutes or more have passed.

test.java


import java.io.File;
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class test{

    public static void main(String[] args){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String filepath = "C:/tedkuma/BOX/";

        File dir = new File(filepath);
        File[] list = dir.listFiles();

        Calendar st = Calendar.getInstance();//Get the current date and time with the Calendar class
        st.add(Calendar.DATE, -1);           //Get 1 day ago
        Date start = st.getTime();           //Change to Date

        Calendar en = Calendar.getInstance();//Get the current date and time with the Calendar class
        en.add(Calendar.MINUTE, -10);        //Get 10 minutes ago
        Date end = en.getTime();

        for(int i=0; i<list.length; i++) {  //Execute processing for files in the target path
          String filename = list[i].getName();
          File targetFile = new File(filepath+filename);
          Long lastModified = targetFile.lastModified();
          Date koushin = new Date(lastModified);

          if(start.compareTo(koushin)<0 && end.compareTo(koushin)>0){//Compare with compareTo
            String update_time = simpleDateFormat.format(koushin);
            System.out.println(update_time+":"+filename);
          }
        }
    }
}

Let's run it. Except for the file being updated, the files within 1 day of the update could be obtained. 3.png

Recommended Posts

Memo: [Java] Identify files with an update date of 1 day or less
[Java1.8 +] Get the date of the next x day of the week with LocalDate
First touch of the Files class (or Java 8)
Java Kuche Day memo
Initialization with an empty string to an instance of Java String type
4th day of java learning
Java study memo 2 with Progate