开发者

weird issue coverting millisecond to java Date [closed]

开发者 https://www.devze.com 2023-03-20 06:01 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

hi im trying to convert a date value in milliseconds granuality into java Date using

new Date( millsecs) the converted value i get is 3 hours behind what it is supposed to be.

I tried using online tools to conver the millisec value i have and it convert to correct date. Can some one poin开发者_C百科t out what im missing!!!

thnx


A java.util.Date does not have an hour -- at least not in the way that you probably mean. Try the following, and you'll see that the date simply is a point in time that you can specific as X milliseconds since epoch:

long millisec = System.currentTimeMillis();
Date date = new Date(millisec);
long millisec2 = date.getTime();

If I print this date in New York City's time zone, it will correspond to some hour of the day. If I print the same date in GMT, then the hour will be four larger. You probably are printing the value in such a way that you see the same time zone effect.

Think of a date as a point in time. That's a specific number of milliseconds since epoch. Let's pick 3PM PDT as our point in time. This corresponds to 6PM EDT. In other words, all three of those values (millseconds since epoch, 3PM PDT, and 6PM EDT) occupy the same spot on on a timeline.

Or, here's another explanation. 3PM PDT on some day is NOT the same as 3PM EDT on that day. Let's say 3PM PDT corresponds to M milliseconds since epoch. Then, 3PM EDT = M - 10,800,000 milliseconds (that's the number of milleseconds in three hours).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号