Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions jre_emul/Classes/java/nio/file/attribute/FileTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package java.nio.file.attribute;

import java.time.Instant;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -52,6 +53,40 @@ public static FileTime fromMillis(long value) {
return new FileTime(value);
}

public native Instant toInstant() /*-[
id p = [JavaNioFileAttributeFileTime getProvider];
if (p) {
return [p toInstantWithJavaNioFileAttributeFileTime:self];
}
@throw create_JavaLangUnsupportedOperationException_initWithNSString_(@"java.time is not available");
]-*/;

public static FileTime from(Instant instant) {
return getProvider().fromInstant(instant);
}

private static Provider provider;

private static Provider getProvider() {
if (provider == null) {
try {
Class.forName("java.nio.file.attribute.FileTimeProvider").newInstance();
} catch (Exception e) {
throw new UnsupportedOperationException("java.time is not available", e);
}
}
return provider;
}

public interface Provider {
Object toInstant(FileTime fileTime);
FileTime fromInstant(Object instant);
}

public static void setProvider(Provider p) {
provider = p;
}

@Override
public int compareTo(FileTime o) {
if (o.millis < millis) {
Expand Down
19 changes: 19 additions & 0 deletions jre_emul/Classes/java/nio/file/attribute/FileTimeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package java.nio.file.attribute;

import java.time.Instant;

public class FileTimeProvider implements FileTime.Provider {
public FileTimeProvider() {
FileTime.setProvider(this);
}

@Override
public Object toInstant(FileTime fileTime) {
return Instant.ofEpochMilli(fileTime.toMillis());
}

@Override
public FileTime fromInstant(Object instant) {
return FileTime.fromMillis(((Instant) instant).toEpochMilli());
}
}