-
如果FTP server和Web Server同一个时区
使用方法org.apache.commons.net.ftp.FTPClient#getModificationTime
public String getModificationTime(String pathname)
throws IOException
Issue the FTP MDTM command (not supported by all servers) to retrieve the last modification time of a file. The modification string should be in the ISO 3077 form “YYYYMMDDhhmmss(.xxx)?”. The timestamp represented should also be in GMT, but not all FTP servers honour this.
Parameters:
pathname – The file path to query.
Returns:
A string representing the last file modification time in YYYYMMDDhhmmss format.
Throws:
IOException – if an I/O error occurs.
Since:
2.0该函数会获取
GMT
时间,格式为:213 20160125035757
。这时间和北京时间会差8个小时。按空格拆分字符串后使用如下方法获取北京时间20160125115757
,主要是格式化添加时区sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
,而不能按照默认的北京时间。因为FTP server和Web Server同一个时区,所以时间是一致的。
public static long getLongFromGMTDateString(String gmtDate, String format) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //默认就是系统设置时区
Date date = sdf.parse(gmtDate);
return date.getTime(); // Unix时间戳
}
-
如果FTP server和Web Server不在同一个时区
需要结合另外第一个方法org.apache.commons.net.ftp.FTPFile#getModificationTime
public Calendar getTimestamp()
Returns the file timestamp. This usually the last modification time.
Returns:
A Calendar instance representing the file timestamp.这可以获取FTP时区的的最后修改时间,但是没有秒,结合方法
getModificationTime
获取秒,拼接起来就可以获取完整的FPT文件最后修改时间。
常识
- Unix时间戳 1453694277000
->北京时间 2016/1/25 11:57:57
-> GMT 2016/1/25 03:57:57 - GMT –>北京时间
GMT-> Unix时间戳 -> 北京时间