Java日历的奇怪阅读

发布于 2024-12-12 02:06:36 字数 656 浏览 0 评论 0原文

有谁经历过 Java 日历中如此奇怪的读数吗?以下代码片段是用 Groovy(在 Grails 中)编写的

Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("Asia/Singapore"))    
cal.setTime(this.timeEnd)
def endHour = cal.get(Calendar.HOUR_OF_DAY)
def endMinute = cal.get(Calendar.MINUTE)
println "cal gettime ${cal.getTime()} -timeend- ${this.timeEnd} end hour!!! $endHour && $endMinute"

,它得到了以下结果

cal gettime Thu Jan 01 16:20:00 GMT+08:00 1970 -timeend- 1970-01-01 16:20:00.0 结束时间!!! 15&& 50

虽然我的日期显示 16:20,但检索单个字段会得到 15:50。 有人有什么想法吗?

谢谢你, 罗伯特

has anyone experienced such a strange reading from Java Calendar? The following snippet is written in Groovy (in Grails)

Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("Asia/Singapore"))    
cal.setTime(this.timeEnd)
def endHour = cal.get(Calendar.HOUR_OF_DAY)
def endMinute = cal.get(Calendar.MINUTE)
println "cal gettime ${cal.getTime()} -timeend- ${this.timeEnd} end hour!!! $endHour && $endMinute"

And it gets me the following result

cal gettime Thu Jan 01 16:20:00 GMT+08:00 1970 -timeend- 1970-01-01
16:20:00.0 end hour!!! 15 && 50

while my date shows 16:20, retrieving the individual field gives me 15:50.
Anyone has any idea?

Thank you,
Robert

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

去了角落 2024-12-19 02:06:36

因为 ${cal.getTime()} 和 ${this.timeEnd} 是日期值,并且格式化为默认时区。您需要 SimpleDateFormat 来显示特定时区的日期。

Because ${cal.getTime()} and ${this.timeEnd} are Date values, and are formatted for the default timezone. You need a SimpleDateFormat to display a Date for a specific TimeZone.

暖伴 2024-12-19 02:06:36

this.timeEnd 时区更改为 “亚洲/新加坡” 可能会使您的初始时间推迟半小时。

timeanddate.com 上检查您的两个时区

Changing the time zone of this.timeEnd to "Asia/Singapore" may have put back your initial time by half an hour.

Check your two time zones on timeanddate.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文