【发布时间】:2013-10-16 15:42:49
【问题描述】:
我正在尝试使用 Groovy 将时间戳转换为格式为 DD/MM/YYYY 的日期
为此,我最初从数据库查询中获取了一个时间戳值。它的格式为:
YYYY-MM-DD HH:MM:SS.S (the .S is actually a milisecond I guess)
所以我创建了一个新变量,并使用getTime()方法将其转换为毫秒,如下所示:
def long myDate = theDate.getTime()
现在,我在这里得到的一些示例是:[添加了两个字段,以便您可以看到我正在转换的内容]
theDate (timeStamp) myDate (time in milis)
1987-01-23 00:00:00.0 : 538358400000
1959-08-26 00:00:00.0 : -326682000000
1982-12-31 00:00:00.0 : 410140800000
现在当我尝试使用 DD/MM/YYYY 格式创建新日期时
def dt = new Date(myDate)
dt = dt.format("DD/MM/YYYY")
这最终是我需要的,我得到以下值:
theDate (timeStamp) myDate (time in milis) new formatted date
1987-01-23 00:00:00.0 : 538358400000 : 23/01/1987 (Perfect)
1959-08-26 00:00:00.0 : -326682000000 : 238/08/1959 (Day is off)
1982-12-31 00:00:00.0 : 410140800000 : 365/12/1982 (Day is off)
所以我的问题是为什么这会返回奇怪的值,我需要更改什么来修复它?
谢谢
【问题讨论】:
标签: date groovy timestamp type-conversion