This discussion is archived
2 Replies Latest reply: Mar 14, 2007 10:11 AM by DrClap RSS

problems with Calendar

807606 Newbie
Currently Being Moderated
I have noticed some calendar anomalies.
If I run the following code:
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;

public class TimeTest {
      public static void main (String[] args) {
            GregorianCalendar cal = new GregorianCalendar();
            Calendar cal2 = Calendar.getInstance();
            Date now = new Date();
            System.out.println ("Month = " + cal.get(cal.MONTH) + ", " + cal2.get(cal2.MONTH));
            System.out.println ("Hour = " + cal.get(cal.HOUR) + ", HOUR_OF_DAY = " + cal.get(cal.HOUR_OF_DAY));
I get an output of 2 for the month, and one hour later than the current time for the hour. Does anyone know what could be causing this?

Many thanks,

  • 1. Re: problems with Calendar
    569573 Newbie
    Currently Being Moderated
    For MONTH, JANUARY is 0, MARCH is 2
    For HOUR, ok for me , what did you get ?
  • 2. Re: problems with Calendar
    DrClap Expert
    Currently Being Moderated
    The 2 for the month isn't an anomaly, it's just the value of the Calendar.MARCH constant. Some genius decided to number the months starting with zero instead of 1 when designing the Calendar class.

    As for the off by 1 hour, sounds like you aren't using a JRE that has the current timezone information in it. Or you didn't update your operating system to have the current information.