Get the "last modified" date from a fileTag(s): IO


To display the value of File.lastModified()
import java.io.File;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class FiledateDemo {
   public static void main(String[] args) {

      File f = new File("c:/temp/output.txt");
      Long lastModified = f.lastModified();

      // java 8
      LocalDate fileDate = Instant.ofEpochMilli(lastModified)
            .atZone(ZoneId.systemDefault())
            .toLocalDate();
      System.out.println(fileDate);

      // the old way
      String  lastmod = new SimpleDateFormat("yyyy-MM-dd").format(new Date(lastModified));
      System.out.println(lastmod);
   }
}


comment Comments