Reversing GregorianCalender object's add minute method

When adding one minute to a GregorianCalender object, we do like below which adds 1 minute to the time:

GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
gc.add(Calendar.MINUTE,1);

But by mistake, I reversed it like:

GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
gc.add(1,Calendar.MINUTE);

Which added 12 years to the time. Can anyone please describe why this happened? My knowledge of Java is not good, so I am just curious why this happened.

Jon Skeet
people
quotationmark

Sure.

  • The constant value of Calendar.MINUTE is 12
  • The constant value of Calendar.YEAR is 1

So your second call is equivalent to:

gc.add(Calendar.YEAR, 12);

And this is why we try not to build APIs like this, of course. java.util.Calendar is a horrible API in any number of ways. Use Joda Time or java.time from Java 8 instead.

people

See more on this question at Stackoverflow