Avoid Java Calendar as much as possible

tl;dr

For Java date processing, use LocalDate class </ font> rather than Calendar class.

that's all.

Story

This is a real story that happened at my place of employment.

Event

On Friday, May 31, 2019, the CI that I regularly run on my project suddenly dropped.

I use Jenkins to run all the tests of the source code written in Java 8 four times a day, but it suddenly started to throw an error when it was all green until yesterday.

Looking at the error log, the relevant part is the part where the Calendar class of the Java standard library is used. The usage is to get the number of days in the current month.

In the test code, June 2017 was mocked as the current date, but the code to be tested is as follows. (* * This is just a sample and is different from the actual code. *)

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.YEAR, LocalDate.now().getYear());
calenar.set(Calendar.MONTH, LocalDate.now().getMonth() - 1);  //Since the month starts from 0, set 0 for January, for example.

//Get the number of days of the month
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

Since it's June, of course, 30 is the return value, but when I debugged it, it was 31.

problem

The test code that had been running since the beginning of development a few years ago suddenly turned red, so I wondered if there was any modification in the library, but I tried various things. I can't find any such articles. ..

If you enter "Java calendar b" in Google search, there seems to be some problem so that "Java calendar bug" is recommended.

スクリーンショット 2019-06-03 4.56.03.png

I couldn't find an article like that in particular, but it's not officially deprecated, but it's not highly recommended. I found an article like ** Let's use LocalDate introduced in Java8 **.

solution

Use LocalDate # lengthOfMonth.

In the first place, since LocalDate is used in the process of acquiring the current date, it is a good idea to use the method of LocalDate class as it is, but a few years ago, the use of LocalData introduced in Java 8 has become widespread. Probably not.

Just do the following:

today = LocalDate.now();

//Get the number of days of the month
int days = today.lengthOfMonth();

With this, CI can be turned around safely. You can work with peace of mind.

end.

Recommended Posts

Avoid Java Calendar as much as possible
Avoid database access from loops as much as possible
Check two unaligned arrays to match as neatly as possible (Java)
The story of dieting the container of the Elixir application as much as possible
Avoid Yubaba's error in Java
Save Java HTML as PDF
JavaScript as seen from Java