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);
    }
}
  • 2
    Yet another example of how bad Java's Date + Calender API is... – Steve McLeod Sep 16 '09 at 08:03
  • any idea in defence of why it is the way it is? – shahensha Apr 06 '11 at 08:02
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Mar 21 '17 at 06:46

4 Answers4

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));
Marc Novakowski
  • 44,628
  • 11
  • 58
  • 63
  • I imagine not being used to Javadoc conventions, http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#HOUR_OF_DAY is 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 Kormos Jan 02 '09 at 16:53
9

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
  • 6,776
  • 1
  • 26
  • 25
1

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--------------- ;)

0

tl;dr

ZonedDateTime.now( ZoneId.of( "America/Montreal" ) )
             .getMinute()

34
( if current moment were 2016-01-23T12:34:56.789-05:00[America/Montreal] )

Details

The other Answers are correct.

java.time

The modern way to do this work is with the java.time classes that supplant the troublesome old legacy date-time classes.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( z );  // zdt.toString(): 2016-01-23T12:34:56.789-05:00[America/Montreal]
int minute = zdt.getMinute();  // 34

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154