5

Anyone know why the MINUTE method in java.util.Caldendar returns an incorrect minute?

import java.util.Calendar;

public class Clock
{
    // Instance fields
    private Calendar time;

    /**
     * Constructor. Starts the clock at the current operating system time
     */
    public Clock()
    {
       System.out.println(this.time.HOUR_OF_DAY+":"+this.time.MINUTE);
    }
}
4

4 回答 4

26

Calendar.MINUTE isn't the actual minute but rather a constant index value to pass into "get()" in order to get the value. For example:

System.out.println(this.time.get(Calendar.HOUR_OF_DAY) + ":" + this.time.get(Calendar.MINUTE));
于 2008-12-31T23:42:49.333 回答
9

HOUR_OF_DAY并且MINUTE是公共静态字段,旨在传递给Calendar#get(int). 通常,您希望使用Calendar.getInstance().get(Calendar.MINUTE)当前分钟。不过,在您的示例中,您想要time.get(Calendar.MINUTE).

于 2008-12-31T23:44:13.463 回答
1

导入 java.util.Calendar;

public class Clock { // 实例字段 private Calendar time;

/**
 * Constructor. Starts the clock at the current operating system time
 */
public Clock()
{
   time = Calendar.getInstance(); 
   System.out.println(this.time.get(Calendar.HOUR_OF_DAY) +
                       ":" + this.time.get(Calendar.MINUTE));
}

}

----0--------------- ;)

于 2009-09-16T07:51:31.647 回答
0
于 2016-12-27T03:06:45.813 回答