用Calendar类输出当前小时(12小时制):
Calendar c = Calendar.getInstance();
System.out.println(c.HOUR);
输出:
10
然而跑这个程序的时候是下午1点
请问这是怎么回事?
用Calendar类输出当前小时(12小时制):
Calendar c = Calendar.getInstance();
System.out.println(c.HOUR);
输出:
10
然而跑这个程序的时候是下午1点
请问这是怎么回事?
乍看一眼,还以为是时区问题
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* hour of the morning or afternoon. <code>HOUR</code> is used for the
* 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12.
* E.g., at 10:04:15.250 PM the <code>HOUR</code> is 10.
*
* @see #AM_PM
* @see #HOUR_OF_DAY
*/
public final static int HOUR = 10;
java.util.Calendar类里的这个HOUR ,不是指的当前小时,只是一个常量而已,你这样打印出来肯定就永远都是10咯
应该这样获取当前小时
Calendar c = Calendar.getInstance(Locale.CHINA);
System.out.println(c.get(c.HOUR));