Java获取某年某季度的第一天出错

时间:2024-01-20 19:15:15

1、错误描述

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
	at java.text.DateFormat.format(Unknown Source)
	at java.text.Format.format(Unknown Source)
	at com.you.freemarker.model.FirstDayOfQuarter.getFirstDayOfQuarter(FirstDayOfQuarter.java:50)
	at com.you.freemarker.model.FirstDayOfQuarter.main(FirstDayOfQuarter.java:64)

2、错误原因

//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String firstDayOfQuarter = sdf.format(FisrtDayOfMonth.getFisrtDayOfMonth(year, month));

而FisrtDayOfMonth.getFisrtDayOfMonth(year, month)是String类型的,format()方法内的事对象或者是Date类

3、解决办法

将FisrtDayOfMonth.getFisrtDayOfMonth(year, month)转化为Date