Goutham
Goutham

Reputation: 93

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.

A file named abc.txt (last modified - July 23 2014 1:00:00 AM ) is present in a US FTP Server. 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 )

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);

Upvotes: 0

Views: 768

Answers (2)

abhati
abhati

Reputation: 307

You can try this :

  1. Create a 1 KB file .
  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).

Upvotes: 0

raj
raj

Reputation: 3811

use File.lastModified() to get last modified, System.currentTimeMillis() to get CurrentTime. Subtract them to get the diff in milliseconds.

diff_in_mins = diff_in_milli_seconds/1000/60;

Upvotes: 1

Related Questions