Question

Date now = new Date();
long timeInterval = now.getTime() - (15705 * 24 * 60 * 60 * 1000L);
long hours = timeInterval / (60 * 60 * 1000L);
LOG.debug(String.format("current date:%s, timeInterval:%d,hours:%d",now.toString(),timeInterval, hours));

The result that system print is(15705 means the number of days since 1970s):

12/12/31 22:06:47 DEBUG stat.TimeTest: current date:Mon Dec 31 22:06:47 CST 2012, timeInterval:50807153, hours:14

You can see the current hour is 21 hours, but the result displays as 14 hours.

Was it helpful?

Solution 2

now.getTime() would get you the value in UTC millis - thats GMT+0. the log print you showed probably uses the system time zone, where it was 22:06:47 and probably wasnt anywhere near england :-)

also, please use the Calendar class for date arithmatic because it, unlike your code, would take into account things like leap years, leap seconds and timezone changes (wehich dont happen in UTC, might mihg thappen in any other zone)

OTHER TIPS

Mon Dec 31 22:06:47 CST 2012 is Mon Dec 31 14:06:47 2012 in GMT time, which is the time zone used for the start of the epoch.

In other words, now.getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT and you use a different time zone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top