tags:

views:

1031

answers:

3

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);
    }
}
+21  A: 

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));
Marc Novakowski
I imagine not being used to Javadoc conventions,http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#HOUR_OF_DAYis a bit ambiguous."Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22."
Spencer K
I agree - the javadoc could be much clearer.
Marc Novakowski
+9  A: 

HOUR_OF_DAY and MINUTE are public static fields that are meant to be passed into Calendar#get(int). Typically, you'd want to use Calendar.getInstance().get(Calendar.MINUTE) for the current minute. In your example though, you want time.get(Calendar.MINUTE).

Zach Langley
A: 

import java.util.Calendar;

public class Clock { // Instance fields 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--------------- ;)