I tried using Java memo LocalDate

Introduction

I had the opportunity to write a program that detects files older than ○ days in Java, so I'll leave it as a reminder. As an example only.

Use LocalDate

Date is also possible, but try using a newer Local Date.

Long lastModified = targetFile.lastModified();
    LocalDate lastDate = Instant.ofEpochMilli(lastModified).atZone(ZoneId.systemDefault()).toLocalDate();

This gets the last modified date from the target file declared in the file class. Image of converting epoch time to LocalDate type.

LocalDate daysBefore = LocalDate.now().minusDays(5); 

Get today's date with now () of LocalDate class. This time, the date 5 days ago is targeted, and it can be obtained with minusDays (5).

if (lastModified.isBefore(base) || lastModified.isEqual(base))

if (lastModified.compareTo(base) <= 0)

if (lastModified.until(LocalDate.now(), ChronoUnit.DAYS) >= 5)

After that, if you write an if statement like this, you should be able to burn files before the specified date. Please note that isBefore () alone can only be taken more than 5 days ago. I'll try to get it 5 days ago with isEqual ().

Postscript Comments from @swordone The conditional expression of the if statement was added.

Summary

It would be nice to pop the File class here. If you have any suggestions, please comment.

Recommended Posts

I tried using Java memo LocalDate
I tried using Java REPL
I tried using JWT in Java
I tried using GoogleHttpClient of Java
I tried using Elasticsearch API in Java
I tried using OpenCV with Java + Tomcat
I tried using Gson
I tried using TestNG
I tried using Galasa
I tried Drools (Java, InputStream)
I tried using Apache Wicket
I tried metaprogramming in Java
I tried to operate SQS using AWS Java SDK
I tried using Log4j2 on a Java EE server
I tried using Spring + Mybatis + DbUnit
Java memo
I tried to interact with Java
I tried UDP communication with Java
I tried the Java framework "Quarkus"
I tried to convert a string to a LocalDate type in Java
I tried to summarize Java learning (1)
I tried using Dapr in Java to facilitate microservice development
[Android] I tried using Coordinator Layout.
I tried using Pari gp container
I tried using WebAssembly Stadio (2018/4/17 version)
I tried to summarize Java 8 now
I tried using Realm with Swift UI
I tried using Java's diagnostic tool Arthas
I tried using UICollectionViewListCell added from Xcode12.
I tried Cassandra's Object Mapper for Java
Java9 was included, so I tried jshell.
I tried using Scalar DL with Docker
[Java] How to calculate age using LocalDate
I tried the new era in Java
I tried using OnlineConverter with SpringBoot + JODConverter
It's new, but I tried using Groonga
I tried Google's entrance exam (unofficial) [java]
java anything memo
Java Silver memo
java, maven memo
Java SE 7 memo
I tried Spring.
java anything memo 2
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried to make a talk application in Java using AI "A3RT"
I tried FizzBuzz.
Java specification memo
I tried JHipster 5.1
Java pattern memo
I tried using Docker for the first time
I tried using Junit on Mac VScode Maven
[For beginners] I tried using DBUnit in Eclipse
I tried putting Java on my Mac easily
I tried barcode scanning using Rails + React + QuaggaJS
I tried to make Basic authentication with Java
[For beginners] I tried using JUnit 5 in Eclipse
java I tried to break a simple block
[Android] I quit SQLite and tried using Realm
I tried hitting a Java method from ABCL