从Groovy中的时间戳获取日期

编程入门 行业动态 更新时间:2024-10-27 18:20:52
本文介绍了从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中的时间戳获取日期

本文发布于:2023-10-16 12:31:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   时间   Groovy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!