简体   繁体   English

用Java计算时差

[英]Calculating Time Difference in Java

I have an requirement to calculate the time difference between Lastmodified Time of a file and currentTime of FTP sever on which the file is present. 我需要计算文件的上次修改时间与存在该文件的FTP服务器的currentTime之间的时间差。

A file named abc.txt (last modified - July 23 2014 1:00:00 AM ) is present in a US FTP Server. 美国FTP服务器中存在一个名为abc.txt(最后修改-2014年7月23日1:00:00)的文件。 I need to get the last modified time stamp (t1) of the file & currentTime of the US FTP server(t2) and calculate the difference in minutes between the two times ( d1,d2 ) 我需要获取文件的最后修改的时间戳(t1)和US FTP服务器的currentTime(t2),并计算两次之间的分钟差(d1,d2)

Here's what I have so far. 到目前为止,这就是我所拥有的。

Calendar zonedate = Calendar.getInstance(); 
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("MST")); 
System.out.println("PDT :: "+ formatter.format(zonedate.getTime())); 
String d1 = formatter.format(zonedate.getTime()); 
System.out.println(d1); 
Date d2 = formatter.parse(d1); 
System.out.println(d2);

use File.lastModified() to get last modified, System.currentTimeMillis() to get CurrentTime. 使用File.lastModified()获取上次修改,使用System.currentTimeMillis()获取CurrentTime。 Subtract them to get the diff in milliseconds. 减去它们可得到以毫秒为单位的差异。

diff_in_mins = diff_in_milli_seconds/1000/60; diff_in_mins = diff_in_milli_seconds / 1000/60;

You can try this : 您可以尝试以下方法:

  1. Create a 1 KB file . 创建一个1 KB的文件。
  2. Upload the file to the server.Note the time taken to upload the file. 将文件上传到服务器。注意上传文件所花费的时间。
  3. Fetch the file again and look at the timestamp it gets.This timestamp minus the time taken to upload the file should give you the server time (approx). 再次获取文件并查看它获得的时间戳。此时间戳减去上载文件所花费的时间应为您提供服务器时间(大约)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM