Java日历时间,分钟是错误的

5 java time

有人知道为什么java.util.Caldendar中的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ski 26

Calendar.MINUTE不是实际的分钟,而是传递到"get()"以获取值的常量索引值.例如:

System.out.println(this.time.get(Calendar.HOUR_OF_DAY) + ":" + this.time.get(Calendar.MINUTE));
Run Code Online (Sandbox Code Playgroud)

  • 我同意 - javadoc可以更加清晰. (2认同)

Zac*_*ley 9

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