I'm trying to do a simple subtraction of dates and getting odd results. For some reason when I format it with SimpleDateFormat, there are 7 extra hours difference.
package timedemo;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Timedemo {
public static void main(String[] args) {
Date start = new Date(); // time right now
Date stop = new Date();
long startTime = start.getTime()-1000; // introduce a second of skew
long stopTime = stop.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
// First one shows up prior to 1970 epoch. Okay, except for 5 hour variance.
// Probably a timezone thing, as I'm in EST (-5).
System.out.println("New date is "+new Date(stopTime - startTime));
System.out.println("Raw Start is "+startTime); // fine
System.out.println("Raw Stop is "+stopTime); // fine
System.out.println("Raw Difference is "+(stopTime-startTime));
System.out.println("Formatted Start is "+sdf.format(startTime));
System.out.println("Formatted Stop is "+sdf.format(stopTime));
System.out.println("Formatted Difference is "+sdf.format(stopTime-startTime));
}
}
And the results are:
New date is Wed Dec 31 19:00:01 EST 1969
Raw Start is 1418397344360
Raw Stop is 1418397345360
Raw Difference is 1000
Formatted Start is 10:15:44
Formatted Stop is 10:15:45
Formatted Difference is 07:00:01
- I had thought it was a timezone thing, but I'm in EST (-5), not MST (-7).
- I would suspect Daylight Savings, but it's 7 hours, not 1.
- 12/24 hour difference? 12-7=5 which is my timezone offset... not sure what to make of it though.
- Kind of out of ideas at this point.
Why the seven hour shift on the last line?