Robert A Henru
Robert A Henru

Reputation: 2272

Strange reading with Java calendar

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

Upvotes: 0

Views: 197

Answers (2)

Maurice Perry
Maurice Perry

Reputation: 32831

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.

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53839

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

Upvotes: 0

Related Questions