我有一个 API 代码,可以在文本文件中写入 token 。我需要检查上次更新时间和当前系统。如果差异为 20 分钟,它将生成一个新 token 。
问题是当我使用以下代码时我没有得到任何区别。如何以整数值的形式获得这些以分钟为单位的差异?
java.nio.file.Path path = Paths.get("C://Users//xxx//token.txt");
attributes = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("Updated Time : " + attributes.lastModifiedTime());
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
最佳答案
转换为Instant
并使用Instant.isAfter()
进行比较方法。不要转换为 String
,这仅在显示人类可读时间时有用。
Path path = Paths.get("C://Users//xxx//token.txt");
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
Instant deadline = Instant.now().minus(20, ChronoUnit.MINUTES);
boolean itsTime = attributes.lastModifiedTime().toInstant().isAfter(deadline);
关于java - 如何在java中获取上次修改时间和当前系统日期时间差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54152749/