I have this wired
public static boolean isFirstDayOfMonth(String format, String value) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(value);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
return dayOfMonth == 1;
}
Test and return true:
boolean actual = CommonUtil.isFirstDayOfMonth("yyyy-MM-dd", "2021-02-29");
assertTrue(actual);
The I found out SimpleDateFormat convert the date into 1st of March 2021, although there is not 29th in February in 2021. If I pass in 2021-02-30, which is invalid, but it return correct result.