[JAVA] Calendar.MONTH starts from 0

Why does the month of the Calendar class start with "0"?

One day, I was talking about why the month was off by a month.

int month = Calendar.getInstance().get(Calendar.MONTH);

If you ran this code in February, the month would be "1". If you answer that it starts with "0", isn't it inconvenient why it starts with "0"? It became a story. I hadn't been aware of it until now, but when I was told, it was strange to start with "0"! I was worried about it, and when I asked someone who seemed to know it,

When counting the months in Japan, it is expressed as "January, February, March ..." English-speaking countries are expressed in letters as "January, February, March ..." instead of numbers. Isn't it started with "0" so that it can be easily managed by an array or a program?

I don't know if this is correct, but I see! I thought. When I looked it up online, I found similar questions and answers.

The system I'm mainly in charge of is still running on Java 5 and 6, so If you want to get the current "month", it is often described as follows.

int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
System.out.println(month + "Month");

If you have ** Java8 or later **, you can get the current "month" without writing "+1" by using the LocalDate class.

int month = LocalDate.now().getMonthValue();
System.out.println(month + "Month");

Recommended Posts

Calendar.MONTH starts from 0
Calendar.MONTH
From (form)
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]