File tree Expand file tree Collapse file tree
core-java-modules/core-java-nio
main/java/com/baeldung/creationdate
test/java/com/baeldung/creationdate Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ## Relevant articles:
2+
3+ - [ Determine File Creating Date in Java] ( https://www.baeldung.com/file-creation-date-java )
Original file line number Diff line number Diff line change 1+ package com .baeldung .creationdate ;
2+
3+ import java .io .IOException ;
4+ import java .nio .file .Files ;
5+ import java .nio .file .Path ;
6+ import java .nio .file .attribute .BasicFileAttributes ;
7+ import java .nio .file .attribute .FileTime ;
8+ import java .time .Instant ;
9+ import java .util .Optional ;
10+
11+ public class CreationDateResolver {
12+
13+ public Instant resolveCreationTimeWithBasicAttributes (Path path ) {
14+ try {
15+ final BasicFileAttributes attr = Files .readAttributes (path , BasicFileAttributes .class );
16+ final FileTime fileTime = attr .creationTime ();
17+
18+ return fileTime .toInstant ();
19+ } catch (IOException ex ) {
20+ throw new RuntimeException ("An issue occured went wrong when resolving creation time" , ex );
21+ }
22+ }
23+
24+ public Optional <Instant > resolveCreationTimeWithAttribute (Path path ) {
25+ try {
26+ final FileTime creationTime = (FileTime ) Files .getAttribute (path , "creationTime" );
27+
28+ return Optional
29+ .ofNullable (creationTime )
30+ .map ((FileTime ::toInstant ));
31+ } catch (IOException ex ) {
32+ throw new RuntimeException ("An issue occured went wrong when resolving creation time" , ex );
33+ }
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .creationdate ;
2+
3+ import org .junit .Test ;
4+
5+ import java .io .File ;
6+ import java .nio .file .Path ;
7+ import java .time .Instant ;
8+ import java .util .Optional ;
9+
10+ import static org .junit .Assert .assertTrue ;
11+
12+ public class CreationDateResolverUnitTest {
13+
14+ private final CreationDateResolver creationDateResolver = new CreationDateResolver ();
15+
16+ @ Test
17+ public void givenFile_whenGettingCreationDateTimeFromBasicAttributes_thenReturnDate () throws Exception {
18+
19+ final File file = File .createTempFile ("createdFile" , ".txt" );
20+ final Path path = file .toPath ();
21+
22+ final Instant response = creationDateResolver .resolveCreationTimeWithBasicAttributes (path );
23+
24+ assertTrue (Instant
25+ .now ()
26+ .isAfter (response ));
27+
28+ }
29+
30+ @ Test
31+ public void givenFile_whenGettingCreationDateTimeFromAttribute_thenReturnDate () throws Exception {
32+
33+ final File file = File .createTempFile ("createdFile" , ".txt" );
34+ final Path path = file .toPath ();
35+
36+ final Optional <Instant > response = creationDateResolver .resolveCreationTimeWithAttribute (path );
37+
38+ response .ifPresent ((value ) -> {
39+ assertTrue (Instant
40+ .now ()
41+ .isAfter (value ));
42+ });
43+
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments