[英]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 : 您可以尝试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.