Java日历的奇怪阅读
有谁经历过 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为 ${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.
将
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