我正在尝试设置 HOUR_OF_DAY 字段并更改 GregorianCalendar 日期对象的时区。
GregorianCalendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT+10"));
System.out.println("HOUR: " + date.get(Calendar.HOUR_OF_DAY));
date.set(Calendar.HOUR_OF_DAY, 23);
//date.get(Calendar.HOUR_OF_DAY);
date.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("HOUR: " + date.get(Calendar.HOUR_OF_DAY));
输出:
HOUR: 16
HOUR: 23
由于某些原因,设置不同时区后 HOUR_OF_DAY 的值不会改变。但是,如果我取消注释 date.get 的 HOUR_OF_DAY,一切正常
GregorianCalendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT+10"));
System.out.println("HOUR: " + date.get(Calendar.HOUR_OF_DAY));
date.set(Calendar.HOUR_OF_DAY, 23);
date.get(Calendar.HOUR_OF_DAY); // uncommenting this line will is changing the output
date.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("HOUR: " + date.get(Calendar.HOUR_OF_DAY));
输出:
HOUR: 16
HOUR: 13
这怎么可能?为什么 .get 方法正在改变对象的行为?