Why is January 0 in Java Calendar?

Because it's much easier to do math with months.

1 month after December is January, but to figure this out properly you have to use the number of months and do the math
12 + 1 = 13 // What month is 13?
I know! I can quickly solve this problem by using a modulus of 12.
(12 + 1) % 12 = 1
This works fine for 11 months until November...
(11 + 1) % 12 = 0 // What month is 0?
you can do all of this again by subtracting 1 before adding the month, then modulo, and adding 1 again at the end...aka solving a potential problem.
((11 - 1 + 1) % 12) + 1 = 12 // Lots of magical numbers!
Now let's consider the 0 - 11 months problem.

(0 + 1) % 12 = 1 // February
(1 + 1) % 12 = 2 // March
(2 + 1) % 12 = 3 // April
(3 + 1) % 12 = 4 // May
(4 + 1) % 12 = 5 // June
(5 + 1) % 12 = 6 // July
(6 + 1) % 12 = 7 // August
(7 + 1) % 12 = 8 // September
(8 + 1) % 12 = 9 // October
(9 + 1) % 12 = 10 // November
(10 + 1) % 12 = 11 // December
(11 + 1) % 12 = 0 // January
所有月份的工作都相同,不需要解决。

Guess you like

Origin blog.csdn.net/doublepg13/article/details/127638297