【发布时间】:2021-02-18 01:31:47
【问题描述】:
我有一个程序可以存储托管在 FTP 服务器中的文件的本地副本。程序每天使用以下代码自动检查服务器上的文件是否已更新:
FTPFile remoteFile = ftpClient.mlistFile(remotePath);
Date remoteDate = remoteFile.getTimestamp().getTime();
BasicFileAttributes localFile = Files.readAttributes(Paths.get(localPath), BasicFileAttributes.class);
Date localDate = new Date(localFile.lastModifiedTime().toMillis());
isUpToDate = localDate.compareTo(remoteDate) > 0;
我和我的同事现在对此代码存在分歧。他说如果程序在不同的时区执行,这可能不起作用,我说它会起作用,因为 Java Date 对象不受时区影响,只有 Calendar 的实例受。我对吗 ?他说的对吗?
【问题讨论】:
-
不是你问的,但我建议你不要使用
Date。该课程设计不良且早已过时。而是使用来自java.time, the modern Java date and time API 的Instant。