I built a program that automatically converts February 31st to 28th when getting the last day of the month using Java's Calendar class. However, 31st was output even though February was specified.
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR,2019);
cal.set(Calendar.MONTH,2);
cal.getActualMaximum(Calendar.DATE);//The result is 31
The cause was that the Calendar class treated 0 as January, so when 2 was specified, it was treated as March.
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR,2019);
cal.set(Calendar.MONTH,1);//February
cal.getActualMaximum(Calendar.DATE)//The result is 28
Why don't you treat 1 as January ... I made a note for those who have the same eyes
Recommended Posts