Get Date from timestamp in Groovy

泄露秘密 提交于 2020-01-13 08:43:17

问题


As trivial as it may seem, I cannot find a way to transform a Unix timestamp into a Date object in Groovy.

Let me try with 1280512800, which should become Fri, 30 Jul 2010 18:00:00 GMT

My first attempt was

new Date(1280512800)
// wrong, becomes Thu Jan 15 20:41:52 CET 1970

Then I read that Java timestamps use milliseconds, hence I should use

new Date((long)1280512800 * 1000)
// the cast to long is irrelevant in Groovy, in any case I get
// Thu Jan 08 03:09:05 CET 1970

What is the right way to convert a timestamp to a Date? What if I want to specify the timezone to work in?


回答1:


The problem is it's going with Integers, then the multiplication is causing truncation...

Try:

new Date( 1280512800L * 1000 )

or

new Date( ((long)1280512800) * 1000 )


来源:https://stackoverflow.com/questions/11448341/get-date-from-timestamp-in-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!