Unexpected output with ISO Time (8601)

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.


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)

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.

Need Your Help

How do I install a Python extension module using distutils?

python module distutils

I'm working on a Python package named "lehmer" that includes a bunch of extension modules written in C. Currently, I have a single extension module, "rng". I am using Python's Distutils to build and

Stuck on Sql Query with time interval

sql postgresql relational-database

I want to get best 3 day of users between "2014-07-01" and "2014-08-01"

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.