Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Calendar UTC Hour

807589Nov 3 2008 — edited Nov 4 2008
Hi,

I would like to know how I can get the hour value from my Calendar cal object in UTC time as an integer. I keep getting 15 when I want to see 7 which is seen in the Date object.

import java.util.*;

public class Cal {

public Cal() {

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.UK);
cal.setTimeInMillis(15l * 3600l * 1000l); // 7 AM PST
printCal(cal); // 1970-1 1 15:0:0

Date d = cal.getTime();
System.out.println(d); // Thu Jan 01 07:00:00 PST 1970

System.out.println(cal.get(Calendar.HOUR_OF_DAY)); // returns 15, HELP!
}

public void printCal(Calendar cal) {
System.out.println(cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH)+1) +
" " + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR_OF_DAY) +
":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));
}

public static void main(String [] args) {
new Cal();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2008
Added on Nov 3 2008
1 comment
458 views