/**
* 根据某月遍历这个月的所有日期
* @Author: laohuang
* @Date: 2022/11/18 15:29
*/
public static List<String> getWeekDayInMonth(String date)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> resultList = new ArrayList<>();
resultList.add(date);
Calendar calendar = Calendar.getInstance();
try
{
calendar.setTime(sdf.parse(date));
}
catch (Exception e)
{
e.printStackTrace();
}
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i < days; i++)
{
calendar.add(calendar.DATE, 1);
resultList.add(sdf.format(calendar.getTime()));
}
return resultList;
}
根据某天获取当前月份的每一天
测试代码
public static void main(String[] args) {
for (String s : getWeekDayInMonth("2022-11-01")) {
System.out.println(s);
}
}
输出结果