java 根据年月获取周数、天数
大家好,我是酷酷的韩~
1.根据年月获取周数
/**
* @Description: 根据年月获取周数
* @Param: [year, month]
* @return: java.lang.Integer
* @Author: hanjinqun
* @Date: 2022/1/13
*/
public static Integer getWeeksByYearAndMonth(Integer year, Integer month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
return c.getActualMaximum(Calendar.WEEK_OF_MONTH);
}
2.根据年月获取天数
/**
* @Description: 根据年月获取天数
* @Param: [year, month]
* @return: java.lang.Integer
* @Author: hanjinqun
* @Date: 2022/1/13
*/
public static Integer getDaysByYearAndMonth(Integer year, Integer month) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}
3.main方法测试
public static void main(String[] args) {
System.out.println(getWeeksByYearAndMonth(2022, 1));
System.out.println(getDaysByYearAndMonth(2022, 1));
}
输出:
态度决定一切。 ------酷酷的韩~