[ BAEL-2978 ]: Determine file creation date in java (#7099)
* [ BAEL-2978 ]: Determine file creation date in java
This commit is contained in:
parent
d5f8b4e852
commit
847ad4d0dd
3
core-java-modules/core-java-nio/README.md
Normal file
3
core-java-modules/core-java-nio/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
## Relevant articles:
|
||||
|
||||
- [Determine File Creating Date in Java](https://www.baeldung.com/file-creation-date-java)
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.creationdate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
public class CreationDateResolver {
|
||||
|
||||
public Instant resolveCreationTimeWithBasicAttributes(Path path) {
|
||||
try {
|
||||
final BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
|
||||
final FileTime fileTime = attr.creationTime();
|
||||
|
||||
return fileTime.toInstant();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException("An issue occured went wrong when resolving creation time", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Instant> resolveCreationTimeWithAttribute(Path path) {
|
||||
try {
|
||||
final FileTime creationTime = (FileTime) Files.getAttribute(path, "creationTime");
|
||||
|
||||
return Optional
|
||||
.ofNullable(creationTime)
|
||||
.map((FileTime::toInstant));
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException("An issue occured went wrong when resolving creation time", ex);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.baeldung.creationdate;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CreationDateResolverUnitTest {
|
||||
|
||||
private final CreationDateResolver creationDateResolver = new CreationDateResolver();
|
||||
|
||||
@Test
|
||||
public void givenFile_whenGettingCreationDateTimeFromBasicAttributes_thenReturnDate() throws Exception {
|
||||
|
||||
final File file = File.createTempFile("createdFile", ".txt");
|
||||
final Path path = file.toPath();
|
||||
|
||||
final Instant response = creationDateResolver.resolveCreationTimeWithBasicAttributes(path);
|
||||
|
||||
assertTrue(Instant
|
||||
.now()
|
||||
.isAfter(response));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenGettingCreationDateTimeFromAttribute_thenReturnDate() throws Exception {
|
||||
|
||||
final File file = File.createTempFile("createdFile", ".txt");
|
||||
final Path path = file.toPath();
|
||||
|
||||
final Optional<Instant> response = creationDateResolver.resolveCreationTimeWithAttribute(path);
|
||||
|
||||
response.ifPresent((value) -> {
|
||||
assertTrue(Instant
|
||||
.now()
|
||||
.isAfter(value));
|
||||
});
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user