本文介绍了从Groovy中的时间戳获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
虽然看起来很琐碎,但是我找不到在Groovy中将Unix时间戳转换为Date对象的方法。
As trivial as it may seem, I cannot find a way to transform a Unix timestamp into a Date object in Groovy.
让我尝试使用 1280512800 ,应改为 Fri,2010年7月30日格林尼治标准时间
Let me try with 1280512800, which should become Fri, 30 Jul 2010 18:00:00 GMT
我的第一次尝试是
new Date(1280512800) // wrong, becomes Thu Jan 15 20:41:52 CET 1970然后我读到Java时间戳使用毫秒,因此我应该使用
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?
推荐答案问题是整数的问题,那么乘法会导致截断...
The problem is it's going with Integers, then the multiplication is causing truncation...
尝试:
new Date( 1280512800L * 1000 )或
new Date( ((long)1280512800) * 1000 )更多推荐
从Groovy中的时间戳获取日期
发布评论