从Groovy中的时间戳获取日期 [英] Get Date from timestamp in Groovy

查看:1446
本文介绍了从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中的时间戳获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆