问题:
2023年的2月29日被自动转化为了3月1日
解决
关闭宽松匹配:simpleDateFormat.setLenient(false);
事例
代码
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parse = simpleDateFormat.parse("2023-13-15");
System.out.println(simpleDateFormat.format(parse));
System.out.println("------------------------------");
simpleDateFormat.setLenient(false);
Date parse2 = simpleDateFormat.parse("2023-13-15");
System.out.println(simpleDateFormat.format(parse2));
}
结果
2024-01-15
------------------------------
Exception in thread "main" java.text.ParseException: Unparseable date: "2023-13-15"
at java.text.DateFormat.parse(DateFormat.java:366)
at com.hx.backmarket.insurance.utils.rule.batchapply.IssueDateRule.main(IssueDateRule.java:49)